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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Nilanka DharmadasaPosted Dec 21, 2009, 11:41 PM
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.
minakshiPosted May 3, 2010, 2:53 AM
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.
Sagar SawantPosted May 3, 2010, 2:37 AM
unable to cast object of type 'System.windows.forms.datagridviewrow ' to type 'system.data.datarow'
in windows application
Nilanka DharmadasaPosted Dec 22, 2009, 4:41 AM
If it works for you, can you please spend few seconds to tick 'Do you like this answer' checkbox?
Thanks
minakshiPosted Dec 22, 2009, 4:25 AM
It works for me.
minakshiPosted Dec 21, 2009, 7:44 AM
Nilanka DharmadasaPosted Dec 21, 2009, 7:16 AM
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.
minakshiPosted Dec 21, 2009, 6:56 AM
Thanks,
Minakshi.
Nilanka DharmadasaPosted Dec 21, 2009, 6:49 AM