How to update datagridview - c#

I am working in c# project, i need to update datagridview control after inserting a new record. When i am doing this, the gridview keeps its old values also, what should i do to show only updated records

You need to show what the data source that you are binding to is. If it doesn't have any update functionality (implementing INotifyPropertyChanged, for example), then the grid won't be able to tell when a value changed and update itself.

You haven't posted a snippet of your code for us to analyze so my best guess would be that you are neglecting to re-bind the Data/Gridview after row insert.

You need to call ResetBindings when the bound data source changes unless the data source implements the appropriate notifications to give an update notification to the grid.

Related

Update ASP.NET databound GridView when database changes

I bind my ASP.NET 2.0 GridView control to a datasource at design time in the Visual Designer.
If the database changes, how do I update the GridView control to reflect the changes in the database?
If the grid was programmatically data-bound, I could have got a fresh copy of data and rebound.
Update
I think I need to clarify my question further. If my GridView was bound at design time using a SqlDataSource data control, then how do I update the GridView programmatically? Do I bind it to a new DataSet or do I simply call the GridView.DataBind() method without changing its data source?
I think I tried this in between and simply calling the GridView.DataBind() did it, but I can't be sure.
You can use AJAX to implement same. Place your GridView in a Update Panel. Add a timer and on timer tick you can rebind your Gridview.
As soon as, data is changed in database it is reflected in the gridview with the tick of timer.
There isn't a built in way to do this in .Net that I've seen.
I believe the best thing you can do is create a data-update thread that checks for new Db values periodically and re-binds the grid from the code-behind as necessary

IBindingList change notification

I am trying to create a program (simple grid - with specialized output) that displays data from an IBindingList data source. I can display the data just fine, but I also want to be able to let the user edit the data in, say in a TextBox that is separate from the grid. My problem is that I am unclear as to how to accomplish the "automatic" updating of the data between the two elements (my grid and the editable textbox).
The list's ListChanged event, I believe, will tell me when the list itself has changed, but I need the grid to know when the actual data in the list has changed. The only thing I have stumbled upon to tell me when a change external to the grid has occurred is to hook onto the BindingManagerBase.Bindings[0].Parse event. Not that a user would do this, but if the user has established several textboxes that are all bound to the same field, I think I would have to loop through each BindingManagerBase.Binding entry and hook it?
I am presuming I am not doing this correctly, and there is a more generalized way to accomplish this. I would GREATLY appreciate some direction from anyone who is more familiar with what I am attempting to accomplish.
Thanks in advance,
-Matt
Have a look at at INotifyPropertyChanged. Implement it on the class that is stored in your BindingList to notify said BindingList when properties on an item change.
If you've implemented IBindingList yourself you'll need to do a little more work to hook up to the events for each item in your list, but the provided BindingList will pick up on these events automatically.
You could also use the generic BindingList and notify the edit using ResetItem

Submit changes to database from DatGridView?

I have a DataGridView on my form that I have associated with a data source using the designer. This allows me to customise the columns in the designer, which helps me a lot. My problem is that I now cannot work out how to submit changes made in the grid view to the database.
I have a DataGridView object, a TableAdaptor object and a BindingSource object - I cannot figure out which combination of calls to make to fluck all changes made in the grid to the database. Ideally, I would also be able to refresh the grids with the latest information from the database as well.
Does anyone have any ideas?
Cheers,
Oops - Straigth after posting I stumbled across the solution:
myTableAdaptor.update(MyDataSet);
For me, both the table adaptor and the data set had been auto-generated as class member variables.

Pattern for GridView binded to ObjectDataSource - late save

I have a GridView which I am binding to my service layer.
I want to be able to allow the user to edit the grid, but I do not want to save the grid as the user is clicking update on each row. I would like to update all of the edited/added/deleted rows when the 'save' button for my entire form is submitted.
I have my service layer configured, and the GridView calls update on a per row edit basis, however I want that to happen all at the end when clicking save.
How can I maintain my ObjectData sources references to update, insert, delete but instead of on a per row basis be able to call a save on everything all at once?
Thanks!
If you're using an object data source, then the behavior of the object bound to the object data source is up to you; it doesn't have to save to the database right away. If you want, you can build the database commands you want to execute, then cache them somewhere until the save button is clicked.
Object data source objects should be static or stateless, so you can't cache there. However, this sounds like a reasonable use of Session cache.
Here is a tutorial on asp.net/learn for wrapping updates in a transaction:
http://www.asp.net/Learn/Data-Access/tutorial-63-cs.aspx
The example uses GridView and ObjectDataSource.
This may or may not be useful with subsonic, but it may help others with a similar problem.

Saving a batch of changes from a DataGrid or GridView

I have a set of data that has been displayed as just a simple GridView with the item name being a hyperlink to view details. I'm attempting to update this scenario so certain fields (sortOrder and isApproved) are editable from the main page and do not require visiting each item in the grid.
I have converted the GridView to a DataGrid and have included a TemplateColumn for the columns in question. I have them hooked up to display the values appropriately. At this point, I'm trying to find a way to peek into the DataGrid and it's related data source to determine if the values have changed on the click event of a button. At that point, I could persist those changes back to the respective SharePoint list.
I'm not very familiar with the DataGrid, or GridView for that matter. Can anyone point me in the correct direction on how I could gain access to the data source at an item/row level during a button click event?
If you add an event handler for ItemCommand, you can then access e.Item.DataItem to get at the data for a particular row.
I believe for a GridView the equivalent would be RowCommand, but not sure off the top of my head.
I needed to learn about DataGrid a while back and asked the question about some good tutorials. The post is located at: Good DataGridView tutorial
To see if any values have changed you will probably have to set a store the row id somewhere when something is changed, then go through all the rows that have changed and do your update.
I hope that helps.

Categories