在我的应用程序中,我在datagridview中显示数据库表。现在我想修改和更改一些记录并将此更改保存在数据库中。我怎么能这样做?
答案 0 :(得分:3)
以下是BindingSource的一个示例:
string query = "SELECT * FROM dbo.bimar";
da = new SqlDataAdapter(query, connectionString);
SqlCommandBuilder cBuilder = new SqlCommandBuilder(da);
dt = new DataTable();
da.Fill(dt);
BindingSource bSource = new BindingSource();
bSource.DataSource = dt;
dataGridView1.DataSource = bSource;
在dataGridView1中更改数据时,请更新它:
private void button1_Click(object sender, EventArgs e)
{
da.Update(dt);
}
答案 1 :(得分:2)
TableAdapter是一种方法。
答案 2 :(得分:0)
objDataAdapter.SelectCommand = new SqlCommand();
objDataAdapter.SelectCommand.Connection = objConnection;
objDataAdapter.SelectCommand.CommandText = "select code,name,family,fatherName,age from bimar";
objDataAdapter.SelectCommand.CommandType = CommandType.Text;
objConnection.Open();
objDataAdapter.Fill(objDataSet, "bimar");
objConnection.Close();
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = objDataSet;
dataGridView1.DataMember = "bimar";
我使用此代码在DataGridView上显示我的表,我想在DatagridView上做一些工作之后将数据插回到db