C# datagridview 与 binddingNavigator & BindingSource实现联动,实现单表查看修改保存一例
作者:互联网
学了两个月的WINFORM,越学越感觉C#功能全面,前几天一直在用DATAGRIDVIEW的基本功能,今天尝试了解了 binddingNavigator & BindingSource和DATAGRIDVIEW进行联动,即点击上一下,下一行图标后,下面的textbox文本框的内容可以自动随着变化。
点击修改按钮可以在进行修改;
0# 前提:
定义两个类变量:dt,ds
DataTable dt; BindingSource bs = new BindingSource();
要点:
1#、datagridview的datasource设为 BindingSource,并且将列表中的字段设置DataPropertyName 为DataTable指定的列;
private void BindData() { dataGridView1.DataSource = bs; //把数据源绑定在dataGridView1上 //绑定每列的值显示在DatagridView this.dataGridView1.Columns[0].DataPropertyName = dt.Columns["id"].ToString(); this.dataGridView1.Columns[1].DataPropertyName = dt.Columns["begintime"].ToString(); this.dataGridView1.Columns[2].DataPropertyName = dt.Columns["endtime"].ToString(); this.dataGridView1.Columns[3].DataPropertyName = dt.Columns["clientid"].ToString(); this.dataGridView1.Columns[4].DataPropertyName = dt.Columns["clientname"].ToString(); }
2、将textbox文本框的DataPropertyName 为DataTable指定的列;
this.txtName.DataBindings.Add("Text", bs, "ClientName"); this.txtId.DataBindings.Add("Text", bs, "ClientId"); this.txtGoods.DataBindings.Add("Text", bs, "Goodsid"); this.txtAmount.DataBindings.Add("Text", bs, "amount");
3、将bindingNavigator1的BindingSource = bs,
附上全部代码 ,其中数据库的类定义在DBHelper;
1 using drp.DB; 2 using System; 3 using System.Collections.Generic; 4 using System.ComponentModel; 5 using System.Data; 6 using System.Drawing; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 using System.Windows.Forms; 11 12 namespace drp 13 { 14 public partial class FrmTest : Form 15 { 16 DataTable dt; 17 BindingSource bs = new BindingSource(); 18 public FrmTest() 19 { 20 InitializeComponent(); 21 } 22 23 private void FrmTest_Load(object sender, EventArgs e) 24 { 25 26 string sql = "select * from agreement"; 27 dt = DBHelper.ExecuteQuery(sql); 28 bs.DataSource = dt; 29 bindingNavigator1.BindingSource = bs;//把数据源绑定在bindingNavigator1上 30 //把datagridview 的数据源绑定到bs 31 BindData(); 32 33 this.txtName.DataBindings.Add("Text", bs, "ClientName"); 34 this.txtId.DataBindings.Add("Text", bs, "ClientId"); 35 this.txtGoods.DataBindings.Add("Text", bs, "Goodsid"); 36 this.txtAmount.DataBindings.Add("Text", bs, "amount"); 37 38 } 39 //修改按钮 40 private void button1_Click(object sender, EventArgs e) 41 { 42 this.txtName.Enabled = true; 43 this.txtId.Enabled = true; 44 this.txtGoods.Enabled = true; 45 this.txtAmount.Enabled = true; 46 this.btnEdit.Enabled = false; 47 this.btnSave.Enabled = true; 48 } 49 // 保存
50 private void button2_Click(object sender, EventArgs e) 51 { 52 //接受修改 53 bs.EndEdit(); 54 DBHelper.UpdateDataTable(dt,"agreement"); 55 //将保存按钮改为 不可用 56 this.btnSave.Enabled = false; 57 this.btnEdit.Enabled = true; 58 } 59 60 private void button3_Click(object sender, EventArgs e) 61 { 62 63 } 64 65 private void toolStripSave_Click(object sender, EventArgs e) 66 { 67 bs.EndEdit(); 68 BindData(); 69 DBHelper.UpdateDataTable(dt, "agreement"); 70 } 71 72 private void BindData() 73 { 74 dataGridView1.DataSource = bs; //把数据源绑定在dataGridView1上 75 //绑定每列的值显示在DatagridView 76 this.dataGridView1.Columns[0].DataPropertyName = dt.Columns["id"].ToString(); 77 this.dataGridView1.Columns[1].DataPropertyName = dt.Columns["begintime"].ToString(); 78 this.dataGridView1.Columns[2].DataPropertyName = dt.Columns["endtime"].ToString(); 79 this.dataGridView1.Columns[3].DataPropertyName = dt.Columns["clientid"].ToString(); 80 this.dataGridView1.Columns[4].DataPropertyName = dt.Columns["clientname"].ToString(); 81 } 82 } 83 }
标签:binddingNavigator,C#,DataPropertyName,datagridview,ToString,dataGridView1,bs,dt, 来源: https://www.cnblogs.com/lrzy/p/15928196.html