如何更新DataGridView中的行?

发布于 2024-08-19 07:24:50 字数 125 浏览 3 评论 0原文

我有一个从数据集填充的DataGridView

如何更改 DataGridView 中的任何单元格,并同时更改数据集(和数据库)。

是否有一个示例程序(C# 语言)可供我学习?

I have a DataGridView that is populated from a DataSet.

How can I change any cell in 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北城孤痞 2024-08-26 07:24:50

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.

长亭外,古道边 2024-08-26 07:24:50
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);
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);
千寻… 2024-08-26 07:24:50

如果您希望程序更改 DataGridView 的内容,只需更改基础数据集即可。数据集也有方法将这些更改提交到数据库。

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.

━╋う一瞬間旳綻放 2024-08-26 07:24:50
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);
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);
活泼老夫 2024-08-26 07:24:50

我有一个连接到访问数据库的应用程序。在这个应用程序中,我这样做了:

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();
}

如果你的数据库在sql server中,那么你可以使用SQlDbCommand类。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文