Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Re: Updating DataTable bound to DataGridView
 

Re: Updating DataTable bound to DataGridView

Hi,

I'm updating a record in a DataTable based on a selected cell in a DataGridview. Here is my code:
((DataRowView)dgv.CurrentRow.DataBoundItem)["Name"] = name;

My questions are:
1. Is there a better way to write the above code to access a bound cell/record?
2. Would it be better to update the DataRow instead of the DataRowView or does updating the DataRowView also update the DataRow?


thanks,
Ron
  • Moved byOmegaManMVPThursday, July 23, 2009 11:07 PM (From:Visual C# General)
  •  
myNameIsRon  Thursday, July 23, 2009 9:05 PM
If it is bound, it should already be updated with the selected cell.

But in terms of accessing the data, yes. I often use the DataBoundItem property to get at the data bound to the DataGridView.

Since you are updating the DataBoundItem, you *ARE* updating the DataRow.

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
  • Marked As Answer bymyNameIsRon Friday, July 24, 2009 9:05 PM
  •  
DeborahK  Friday, July 24, 2009 3:55 PM
If it is bound, it should already be updated with the selected cell.

But in terms of accessing the data, yes. I often use the DataBoundItem property to get at the data bound to the DataGridView.

Since you are updating the DataBoundItem, you *ARE* updating the DataRow.

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
  • Marked As Answer bymyNameIsRon Friday, July 24, 2009 9:05 PM
  •  
DeborahK  Friday, July 24, 2009 3:55 PM
Thanks Deborah,

I did notice that updating DataRowView did not work with my Expression column automatically (but updating the DataRow worked):

myDataTable.Columns["Subtotal"].Expression = "Price * Quantity";

// Update the DataRow
((DataRowView)dgv.CurrentRow.DataBoundItem).Row ["Quantity"] = newQuantity;


Ron
myNameIsRon  Friday, July 24, 2009 7:01 PM
Cool. So all is well?
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Friday, July 24, 2009 8:46 PM
All is good, I noticed that when I update the bound DataRow, the DataGridView does not automatically refresh. I use dgv.Refresh(), but let me know if you think there's a better way.

thanks,
Ron
myNameIsRon  Friday, July 24, 2009 9:08 PM
I normally bind to a BindingList instead of a DataSet. The business objects in the BindingList implement INotifyPropertyChanged. So when I update the list, it automatically keeps the UI (grid in this case) in sync.

It sounds like the DataSet does not do that. So it seems that a refresh is in order.


www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Friday, July 24, 2009 9:26 PM

You can use google to search for other answers

Custom Search

More Threads

• i want to convert this code return datatable instead of IList
• Binding Datagridview using ArrayList?
• left top Datagridview button ?
• Another dataGridView coloring rows problem
• programmatically find a row in a DataGridView and select it
• Help with clunky event handling
• .net ODBC Provider
• DataGridView set row header color
• C# working with DataBases
• Quickest way to read data from Access