Update ASP.NET databound GridView when database changes - c#

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

Related

Refresh 2 Gridview using 1 button asp.net webform

Is it possible to refresh two Gridview after an update using a single button? The source of my Gridviews are different stored procedures.
The best solution would be to notify the grid that the object had a modification. To do that, Make your ViewModel implement INotifyPropertyChanged and you can then notify when the property changes (the data structures holding your datas). The grid will then be refreshed
in your button click call the update stored procedure first and after that you can call the fetch data stored procedure and rebind the data to 2nd gridview.

Can I force the CustomUnboundColumnData event to be raised?

I have a usercontrol that has custom unbound data columns in a gridView. I want to retain view state when switching between screens in my app in terms of selected tabs and focused rows etc.
When I do this switch and restore that view state, my custom data is gone and the CustomUnboundColumnData event handler does not get used at all.
Is there a way to raise the CustomUnboundColumnData event for this gridView after I restore my view state?
Call the RefreshRow or RefreshRowCell methods, passing a row handle and optionally a column.
If you are using the ASPxGridView, I would suggest that you turn off the EnableRowsCache property. One more solution is to call the ASPxGridView's DataBind method explicitly within the Page_Init method.
Found a work around. I was refreshing my data in this grid view anyway. However, the few columns that had unbound data needed to be re-added to the grid before refreshing this data.

Dynamically load user control

I have this GridView that have it's DataSource as a list of previously selected products.
For every item on it, I need to Eval it's ID and load a specifc form that the user must fill, then after that i've got to update the GridView.
I have made those specific forms as User Controls, is this the best approach for this scenario?
If yes, how can I dynamically load them, in a way that I can make queries/postbacks then update back my gridview?
What you want to develop is called Master/Detail view.
It is described for example in http://www.asp.net/data-access/tutorials/master-detail-using-a-selectable-master-gridview-with-a-details-detailview-cs .
The only difference that as Detail you need use FormView, where on some initialization event (like DataBound http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basedataboundcontrol.databound.aspx )you need to dynamically create you control and set its parent to current template of FormView

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.

How to update datagridview

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.

Categories