如何在没有按钮的情况下从datagridview更新数据库



如何在没有按钮的情况下从datagridview更新数据库?我想在单元格中写入,并且离开行后应该自动更新吗?知道吗?感谢

我试过了。我不知道是什么事件

personneservice ps = new personneservice();
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
personne p = new personne(id, nom, prenom, sexe, profession, salaire);
ps.update(p);
MessageBox.Show("Bien modifié ");
actualiser();
}

void actualiser ()
{
dataGridView1.Rows.Clear();
foreach (personne p in ps.findAll())
{
String[] row = new String[] { p.Id + "", p.Nom, p.Prenom, p.Sexe, p.Profession, p.Salaire + "" };
dataGridView1.Rows.Add(row);
}
}

这不是我自己的作品,我在网上找到的。

此处:https://social.msdn.microsoft.com/Forums/en-US/231be175-12d3-44ef-9222-875643a9e7fb/saving-the-data-entered-in-the-datagridview-to-database-table?forum=winformsdatacontrols

  1. 在表单中添加DataGridView控件
  2. 选择该控件并打开DataGridView任务,点击">"simbol
  3. 打开"选择数据源"组合框,然后单击"添加项目数据源…">
  4. 选择数据库>下一个>选择/创建连接>下一步>选择要填写控件的表>完成
  5. 这将创建一个DataSet对象。该对象也将与自动生成的Adapter对象一起显示在表单中
  6. 在设计器中选择并右键单击数据集对象控件,然后在数据集设计器中选择"编辑"…>这将打开相关的XSD文件
  7. 右键单击打开的数据集UI,然后选择"配置…">
  8. 选择"高级选项"按钮。。。此窗口将选中所有插入、更新、删除和其他查询的复选框,保持原样,然后按"确定">
  9. 然后按Next并选择"Create methods to send updates directly to the database option"[默认,已选中]>Next并完成它。这将自动生成执行操作所需的代码
  10. 在Form类中添加以下代码

这将填充数据网格视图

private void DataGridViewDirectDBUpdate_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'northwindDataSet.Users' table. You can move, or remove it, as needed.
this.yourtableadaptor.Fill(this.yourdataset.yourtable);
}

这将把数据保存回数据库

private void dataGridView1_CellEndEdit(object sender, EventArgs e)
{
this.yourtableadaptor.Update(yourdataset);
}

相关内容

最新更新