使用BindingSource绑定数据库


1.前言

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

2.绑定数据源

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

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

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

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

对应的在 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(); }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM