update data grid view automatically - c#

I have two windows forms. The first form shows list of records from sql. When you click some cell of a record in DataGridView it shows the second form. In the second form you can edit and update the record. I want DataGridView to be
updated automatically when user close second form. What should I do?

Are you using data bindings, or do you manually fill the grid ? With data bindings its very easy... if the data is contained in a DataTable and you change it somewhere, the change will be reflected automatically in the DataGridView. It also works with objects that implement INotifyPropertyChanged, and lists that implement IBindingList.
If you're not using bindings, you can :
locate the cell that contains the edited value, and update it manually, OR
refill the entire grid

Fill the grid with data from the database again?

Related

how to get values of selected rows in data grid view and store as Dictionary data structure

Window form contains a grid view along
with check box column ,which is populated with data.I want to get values of checked/ selected rows in data grid view and store the values as a form of dictionary data structure for further operation.
Thanks.
You should go the other way round. Instead of tying to extract data from your DataGridView and store it in some collection, first place your data in a collection and then bind this collection to the DataGridView.
First create a data class with properties for all your DataGridView columns.
Then create an ObjectDataSource.
Then fill your data in a List<DataClass>.
Finally assign this list to grid through the DataSource property.
From now on, when edits are made, the changes will automatically be written back to your data objects.
You can do this for binding to Forms as well. This saves you all the effort of formatting and assigning your data to controls and then (after edits have been made) read the controls and convert the strings back to typed data.

How i can refresh wpf form or list view on click event performed on second form while performing updates on it?

I have list view on first forms if i select a row and click edit button second form open their i do updates in selected row and click ok then database get updated but i don't know how to update list view display on first form according to updated database?
I will bite the bullet and give you an answer... note that this will be all I offer, no long discussion!
Your ListView will be bound to an IEnumerable of data objects. You will be passing the selected data object to the second form, so any operation done on that data object in the second form will instantly show up in the first form - because you passed an object it is by reference, both forms are looking at the same thing.
As for updating after the database save - you can save yourself some trouble and only get a fresh copy of the data object if there was a problem with the save (so what is currently in the database doesn't match the contents of the data object), or if there is more than one concurrent user of the database. If this is the case then you know the identifier of the data, just fetch a new copy then replace the data row in the IEnumerable that the ListView is bound to.
Remember: data binding is your friend. And it is easy to insert and remove from a list.

Populate a datagrid from another datagrid in another form

As mentionned in the title, I try to populate a datagrid from data of another one. Basically, The first datagrid is populated from the active directory. I do some operations on these datas, and I would like to make a report of these operations (Passed or failed).
I'd like this report in another datagrid in another winform.
As my operations is to delete parts of the datas I get in my firt datagrid, I cannot populate the second one the same way it works for the first one
So I'd like to get the datas from the first datagrid, add them to another datagrid in the second form, add a column 'Status' and populate it with the results of my operations.
Is there a way to 'copy' the first datagrid ? Is there a best practice to do this kind of job ?
Thanks!
Of course there could be many possible ways, but that came to my mind is.
You know the number of columns, so make a list of class(corresponding to your fields). Extract the cell data from datagrid and populate the list with the entries (if all required). Import this list to the other form. Set Datasourceof datagrid equal to list and refresh.

How can I add one row to a DataGridView?

I work with a WinForms app.
I want to add one row to a DataGridView that this row must enter to database.
What event handler must I use to do this?
Is your DataGridView bound to that database? If so, you should be adding and deleting rows in that data source, not in the grid control itself. Try calling the AddNew method on the binding source.
If you're using a standalone DataGridView that isn't bound to a database, then you can call one of the many overloads of the Add method of the grid control's Rows collection.
This might help you out to add a row to the DataGridView:
http://msdn.microsoft.com/de-de/library/system.windows.forms.datagridview%28v=vs.80%29.aspx
If the DataGridView isn't bound to the database, try to bound it or write the data to the database programmatically.

Inserting new row in gridview using an external Add button and a list as data source

I have a grid with three columns, two of which contain drop-downs, all of them getting filled from a web service result set. Now I want to allow the functionality of adding a new record in the grid by clicking an Add button present outside the gridview.
Whenever the user clicks the Add button, a new record should be created in the grid with value list filled in the drop-downs with the available options.
What is the best possible way to achieve this considering extensibility in mind.
Thanks a lot!
P.S. The data source set for the grid is a list.
Add a blank item to the list, and rebind with this new list and dummy item. That's typically one way to do it, or store the insert form in the footer of the columns. I've used that approach.

Categories