0
votes

Forgive me for this seemingly obvious question, but my searches have not revealed the answer.

I have a DataGridView bound to a DataTable. Although all rows are displayed in the grid, the table represented by the Grid's DataSource does not get updated after changing values in one or more DataGridView columns. Both grid and table contain a Primary Key. What am I missing here? Do I need to do something in the RowValidated event?

    dgvCriticalContacts.AutoGenerateColumns = False
    Dim ccRs As DataTable = Common.OpenRecordset("SELECT * FROM PhoneList")
    dgvCriticalContacts.DataSource = ccRs
1
What is ccRs? Where is the code that update the database? - Steve
ccRS is a DataTable. Must I issue explicit SQL UPDATE statements when cells are changed? I guess the DataGrid does not implicitly perform updates. - ron tornambe

1 Answers

1
votes

A DataGridView does not have the ability to automatically update the database you are getting your information from. While you can use a DataTable to give the DataGridView a data source, it also does not have the ability to update itself back to the database.

However, the .Net Framework does have a TableAdapter ( http://msdn.microsoft.com/en-us/library/bz9tthwx%28v=vs.90%29.aspx, 3.5 and earlier) or a DataAdapter ( http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter%28v=vs.90%29.aspx, All versions) that can perform these functions.

My personal experience is that these adapters very quickly cause more problems than they solve if it is anything more a simple data structure. If it is anything more complex, you are better off creating a class for your rows and making a BindingList (or something similar) to allow the DataGridView to display them. You will have to implement your own change tracking and CRUD statements, but it's well worth it and will likely cause less of a headache in the long run.