开发者

how to update row in DataGridView?

I have a DataGridView that is populated from a DataSet.

How can I change any cell i开发者_开发技巧n the DataGridView, and have this change the DataSet too (and the Database).

Is there a sample program for this (in C#) that I can learn from?


Here is an article with clear explanations, screenshots, and code that demonstrates how to use the DataGridView. The data binding sections should be of particular interest.


DataRowView drv = dataGridView1.CurrentRow.DataBoundItem as DataRowView;
DataRow[] rowsToUpdate = new DataRow[] { drv.Row };

SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Categories", con);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(rowsToUpdate);


If you want a program to change the contents of the DataGridView, just change the underlying Dataset. The DataSet has methods to commit those changes to the Database as well.


DataRowView drv = dataGridView1.CurrentRow.DataBoundItem as DataRowView;
DataRow[] rowsToUpdate = new DataRow[] { drv.Row };

SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Categories", con);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(rowsToUpdate);


I've a app which is connected to access database. In this app, I've done like this:

try
{
 con.Open();

 foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
 {
  OleDbCommand cmd = new OleDbCommand(cmdTxt, con);
  cmd.Parameters.AddWithValue("kurs", c2);
  cmd.Parameters.AddWithValue("ID", item.Cells[0].Value);
  cmd.ExecuteNonQuery();
 }
}
catch (Exception exception)
{
 MessageBox.Show(exception.Message);
}
finally
{
 con.Close();
}

If your database in sql server, then u can use SQlDbCommand class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜