Hello,
Can anyone tell me how to update the changes made to the datagiview control in c#.net windows application.
Thanks,
Minakshi
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Dec 21, 2009, 6:41 PM EST
I;m really sorry. If you want to use that code the datagridview should be databound. Still the code should be corrected in this way.
foreach (DataGridViewRow rw in dataGridView1.Rows)
{
DataRow dr = ((DataRowView)rw.DataBoundItem).Row;
if (dr.RowState == DataRowState.Modified)
{
//Create your update query.
string query = "Update tablename set column1 = '" + dr["column1"].ToString() + "', column2 = '" + dr["column1"].ToString() + "' where ......";
//Then call it.
//Hope you know how to connect to the database and call a sql query.
}
}
But if you cannot use the above code, you will have to use an event like 'CellValueChanged' .
There are two ways to use that. If you want you can save the changes to the database immediately after a cell value change occurs.
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
//update the table
}
But this increases the number of calls to Database and it won't be efficient.
So you can do something else. In cellvaluechanged event you store the updated rows to an arraylist.
Then in buttonclick event(Or at anytime) you read the arraylist and update the relavnt records.
System.Collections.ArrayList rowindexes = new System.Collections.ArrayList();
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if ((e.RowIndex > -1) && (!rowindexes.Contains(e.RowIndex)))
{
rowindexes.Add(e.RowIndex);
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (object i in rowindexes)
{
int index = (int)i;
DataGridViewRow dr = dataGridView1.Rows[index];
//Create your update query.
string query = "Update tablename set column1 = '" + dr.Cells["column1"].ToString() + "', column2 = '" + dr.Cells["column1"].ToString() + "' where ......";
//Then call it.
//Hope you know how to connect to the database and call a sql query.
}
}
If this answer helps you, please do not forget to check 'Do you like this answer' checkbox.
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Dec 21, 2009, 1:49 AM EST
minakshi24 reputationPosted Dec 21, 2009, 1:56 AM EST
Thanks,
Minakshi.
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Dec 21, 2009, 2:16 AM EST
Use following code.
foreach (DataRow dr in dataGridView1.Rows)
{
if (dr.RowState == DataRowState.Modified)
{
//Create your update query.
string query = "Update tablename set column1 = '" + dr["column1"].ToString() + "', column2 = '" + dr["column1"].ToString() + "' where ......";
//Then call it.
//Hope you know how to connect to the database and call a sql query.
}
}
If this answer helps you, please tick 'Do you like this answer' checkbox.
minakshi24 reputationPosted Dec 21, 2009, 2:44 AM EST
minakshi24 reputationPosted Dec 21, 2009, 11:25 PM EST
It works for me.
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Dec 21, 2009, 11:41 PM EST
If it works for you, can you please spend few seconds to tick 'Do you like this answer' checkbox?
Thanks
Sagar Sawant2 reputationPosted May 2, 2010, 10:37 PM EDT
unable to cast object of type 'System.windows.forms.datagridviewrow ' to type 'system.data.datarow'
in windows application
minakshi24 reputationPosted May 2, 2010, 10:53 PM EDT
You cannot assign the datagridviewrow to datarow. Try to use the code that uses the cell value changed event for storing the index of the changed rows into an array and those changes are saved on call of button_click event.
Hope you understand.
Minakshi.