Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Databinding and Grids

Status
Not open for further replies.

BoulderBum

Programmer
Jul 11, 2002
2,179
US
If I have a grid bound to a DataView/Set, and the DataSource changes while on the same form, how do I cause the grid to rebuild according to the changes (notice the changes to the data)?
 
In general, you need to re-bind the grid every time your data source changes. The grid won't "notice" changes and will continue displaying "old" data untill you explicitly set its data source and call DataBind method.
 
Thanks LV, but I actually meant the Windows grid. It doesn't have the same DataBind method as the server control, so I'm a little unclear on how to refresh it (I'm relatively new to Windows programming).
 
Sorry, was thinking ASP .Net grid control.I'm not a Window programming guru either. The windows forms DataGrid control does have the DataSource property though. So perhaps if you explicetly re-point the grid to a changed data source, it'll do? Sorry for not being much of a help.
 
Hi BoulderBum,
There is a lot of documentation related to your question.
1 .Data
A DataGrid object is used , generally , to display ADO.NET data in a scrollable grid.
The data displayed at run time is originated from the followings:
- DataSet/DataTable,
- DataViewManager/DataView
- any custom component that implements IList or IListSource interface.
2. In the case of binding the grid using the DataSet/DataTable/DataView objects, these objects contains data from a database e.g. SQL Server, Oracle etc..
3. The general pattern is :
- Use ADO.NET objects to connect with the source database
- Load the nedeed data from the source database and store it a DataSet/DataTable object
- Disconnect from the source database
- Display data in the DataGrid
- While Editing data presented in the DataGrid object, the datagrid changes are also reflected in the DataSet/DataTable object BUT no in the source database.
- It is the responsability of the application to decide when and how to commit the changes made in the DataSet/DataTable object to the source database.
Here are many ways to commit the changes and that is depending on the many factors. The DataAdapter , CommandBuilder, Transaction could be used as well Stored procedures and triggers.
From my experience do not count on the SQL generated statements which are not performant and use a combination of what DataAdapter, DataSet/DataTable, SqlCommand, OleCommand offer with the power of the stored procedures.
- So this step involves :
.connection with the source database
.commit the changes from the DataSet/DataTable into source tables
.refresh the datagrid data
.disconnect from the source database
4. As an example, the data presented in a datagrid on form become from a complex view object that is stored in a source database e.g. the data in the view object is extracted from many tables which can be from more than one database.
In this case the DataTable object is enough to store the data of this view object and to bind the DataGrid object with this DataTable object.
Any changes in the DataGrid object could be reflected automatically in the DataTable object but it is understood that these changes CANNOT reflected in the same way in the source database tables because.
The solution to commit the changes made in the DataTable object is to have stored procedures called from your application, capture the return code/values.

-obislavu-
 
Thanks, ob, but not quite what I was looking for. I was looking to update a grid on a Windows form, not implement changes in the underlying data model.

As I discovered, in Windows .NET programming there exists something called a "CurrencyManager" which binds object to a data model. It's pretty freakin sweet! You can bind pretty much any property of a control to the data model (e.g. Text of a TextBox) and those bound controls will automatically update along with changes in the current record. For instance, if you have 20 TextBoxes all bound to a data model, and have a "Next" button, the only code needed to update all controls upon a "Next" click is something like:

this.BindingContext[data].Position += 1;

In the context of a DataGrid (Windows) and my question, updates should be able to reflect changes to the data model with a simple call to CurrencyManager.Refresh(), though I haven't actually tried it yet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top