1.前言

在 C# 中我们常见的绑定数据库的方式是通过 ADO.NET 来实现的,除此之外,还可以直接在 form 的设计界面实现对数据源的绑定,这就是使用 BindingSource 实现的方法,下面将介绍这种方法。

2.绑定数据源

在 form 设计界面,点击 DataGridView 右上角的箭头,出现以下界面:

使用BindingSource绑定数据库

在 Choose Data Source 对应的下拉框中选择或者创建数据源,

使用BindingSource绑定数据库

绑定数据源后的结果如下:

使用BindingSource绑定数据库

此时 DataGridView 会自动完成 Column 的创建,项目将会自动为其创建相关的控件,如下图所示:

使用BindingSource绑定数据库

对应的在 Form 的中将显示以下代码:

private void Form1_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'testDataSet.DebtorData' table. You can move, or remove it, as needed.
    this.debtorDataTableAdapter.Fill(this.testDataSet.DebtorData);
}

此时,就完成了通过 BindingSource 实现了数据源的绑定。

3.实现增删改查

暂时没找到。

4.TableAdapter Update

private void Form1_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'testDataSet.T1' table. You can move, or remove it, as needed.
    this.t1TableAdapter.Fill(this.testDataSet.T1);
}

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    t1TableAdapter.Update(testDataSet.T1);
    testDataSet.T1.AcceptChanges();
}

相关文章:

  • 2021-11-17
  • 2021-05-25
  • 2022-12-23
  • 2021-11-26
  • 2022-03-09
  • 2022-02-16
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-05
  • 2022-12-23
  • 2022-12-23
  • 2021-09-03
  • 2022-02-08
  • 2021-06-14
  • 2022-12-23
相关资源
相似解决方案