数据绑定是分离UI和后端主逻辑程序的一种好的办法。这里总结下TextBox, Label, ComboBox, ListBox, DataGridView的数据绑定

数据绑定都是通过DB来和UI控件的属性进行绑定的。因此都需要MySqlDataAdapter的Fill函数得到数据源DataTable并进行绑定

1. TextBox和Label

没有找到与DataTable里任意一行的数据绑定,只能与第一行进行绑定,如果每次都是需要通过MySqlDataAdapter来从数据库取数据,再用Fill函数来给DataTable充数据,再绑定到TextBox,显得太麻烦了。

Label和TextBox一样,都可以通过Control.DataBinding.Add("Text", dt, "Column");就完成绑定了

如果要绑定到一个类的属性上,则用

textBox1.DataBinding.Add("Text", person, "Name", true, DataSourceUpdateMode.OnpropertyChanged);

在程序对类的属性值进行修改后,控件通过textBox.DataBinding["Text"].ReadValue();来更新值,如果在控件上进行修改,离开控件焦点后会自动更新属性值

2. ComboBox和ListBox

comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Name";

注意这里DisplayMember指的是ComboBox上显示的Item值,而ValueMember是实际的值。

ListBox用法跟ComboBox一样

这里可以注意到一个有趣的问题,当ListBox上选择Item的时候,ComboBox,TextBox和Label选的值也跟着一起变了,这可能是因为他们都从同一个数据源绑定的数据,而选择值时是会影响到DataTable的Select方法,进而影响到其他绑定控件,因此可以新建个DataTable,这样就不会影响到了。

ListBox的DataSource还能使enum等

listBox1.DataSource = Enum.GetValues(typeof(Week));

3. DataGridView

bindingSource1.DataSource = dt;
dataGridView1.DataSource = bindingSource1;

如果要通过dataGridView来修改数据库,当然我们可以通过写SQL语句,但是这里可以通过MySqlCommandBuilder myBuilder = new MySqlCommandBuilder(adapter);来修改数据库了

DataGridView修改后,我们首先要确定已经修改完成了,再update到DB,再change DT里

((BindingSource)dataGridView1.DataSource).EndEdit();
adapter.Update(dt);
dt.AcceptChanges();

最后我做了一个demo程序

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
 using System.Diagnostics;
 using MySql.Data;
 using MySql.Data.MySqlClient;

 namespace ControlTest
 {
     public partial class Form1 : Form
     {
         DataTable dt = new DataTable();
         static MySqlConnection mycon = null;
         MySqlDataAdapter adapter;
         public Form1()
         {
             InitializeComponent();
          }

         private void Form1_Load(object sender, EventArgs e)
         {
             mycon = new MySqlConnection("server = 127.0.0.1; uid = root; pwd = 0219; database = persons");
             mycon.Open();
             adapter = new MySqlDataAdapter("select * from students", mycon);
             MySqlCommandBuilder myBuilder = new MySqlCommandBuilder(adapter);
             adapter.Fill(dt);
             textBox1.DataBindings.Add("Text", dt, "Grade");
             label1.DataBindings.Add("Text", dt, "Grade");
             comboBox1.DataSource = dt;
             comboBox1.DisplayMember = "Name";
             comboBox1.ValueMember = "Name";
             DataTable listDt = new DataTable();
             adapter.Fill(listDt);
             listBox1.DataSource = listDt;
             listBox1.DisplayMember = "Name";
             listBox1.ValueMember = "Name";
             bindingSource1.DataSource = dt;
             dataGridView1.DataSource = bindingSource1;
             mycon.Close();
         }

         private void button1_Click(object sender, EventArgs e)
         {
             mycon.Open();
             string query = "update students set Grade = 88 where Name = 'yangzihao'";
             MySqlCommand mycmd = new MySqlCommand(query, mycon);
             mycmd.ExecuteNonQuery();
             dt.Clear();
             adapter.Fill(dt);
             mycon.Close();
         }

         private void button2_Click(object sender, EventArgs e)
         {
             ((BindingSource)dataGridView1.DataSource).EndEdit();
             adapter.Update(dt);
             dt.AcceptChanges();
         }

         private void button3_Click(object sender, EventArgs e)
         {
             MessageBox.Show(comboBox1.SelectedValue.ToString());
         }

     }
 }

C#: 数据绑定的更多相关文章

  1. UWP中新加的数据绑定方式x:Bind分析总结

    UWP中新加的数据绑定方式x:Bind分析总结 0x00 UWP中的x:Bind 由之前有过WPF开发经验,所以在学习UWP的时候直接省略了XAML.数据绑定等几个看着十分眼熟的主题.学习过程中倒是也 ...

  2. MVVM模式和在WPF中的实现(二)数据绑定

    MVVM模式解析和在WPF中的实现(二) 数据绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

  3. 前端MVC学习总结(一)——MVC概要与angular概要、模板与数据绑定

    一.前端MVC概要 1.1.库与框架的区别 框架是一个软件的半成品,在全局范围内给了大的约束.库是工具,在单点上给我们提供功能.框架是依赖库的.AngularJS是框架而jQuery则是库. 1.2. ...

  4. vue双向数据绑定原理探究(附demo)

    昨天被导师叫去研究了一下vue的双向数据绑定原理...本来以为原理的东西都非常高深,没想到vue的双向绑定真的很好理解啊...自己动手写了一个. 传送门 双向绑定的思想 双向数据绑定的思想就是数据层与 ...

  5. 【Win 10应用开发】分阶段进行数据绑定

    使用x:Bind扩展标记进行数据绑定,是在编译阶段完成,至于说性能优化方面,大概主要是优化CPU资源的使用,因为免去了运行阶段进行绑定的过程.当然,使用这个标记仅仅是绑定上的优化,并不包括数据源.数据 ...

  6. WPF入门:数据绑定

    上一篇我们将XAML大概做了个了解 ,这篇将继续学习WPF数据绑定的相关内容 数据源与控件的Binding Binding作为数据传送UI的通道,通过INotityPropertyChanged接口的 ...

  7. SAP CRM 客户控制器与数据绑定

    当用户从视图离开时,视图将失去它的数据.解决这个问题,需要引入客户控制器(Custom Controller)(译者注:SAP CRM客户端中,不同地方的Custom Controller会翻译为“客 ...

  8. AngularJS 系列 01 - HelloWorld和数据绑定

    目录导读: AngularJS 系列 学习笔记 目录篇 前言: 好记性不如烂键盘,随笔就是随手笔记,希望以后有用. 本篇目录: 1. Hello World 2. AngularJS中的数据绑定 3. ...

  9. 双向数据绑定(angular,vue)

    最近github上插件项目更新了关于双向数据绑定的实现方式,关于angular和vue. angular众所周知是使用的脏检查($dirty).一开始大家会认为angular开启了类似setInter ...

  10. WindowsForm多窗体、多窗体传值、控件数据绑定--2016年12月8日

    多窗体 Show Form1 f1 = new Form1(); f1.Show(); ShowDialog--在父窗体之上 Form1 f1 = new Form1(); f1.ShowDialog ...

随机推荐

  1. 【转】c# 解析JSON的几种办法

    http://www.cnblogs.com/ambar/archive/2010/07/13/parse-json-via-csharp.html 刚开始只是想找一个转换JSON数组的方法,结果在M ...

  2. bug

    expected identifier,string or number   //这种问题一般是json数据中最后一个逗号没去掉.

  3. FTS抓包看蓝牙验证的过程

    1.概述    在进行蓝牙设备的连接时,为了保护个人隐私和数据保密的需要,需要进行验证.   2.一些Frame Frame74:本地发送Authentication requset command ...

  4. 蓝牙4.0的LM层说明

    1.概念 The Link Manager Protocol (LMP) is used to control and negotiate all aspects of the operation o ...

  5. php--validate表单验证

    validate表单验证扩展规则 添加自定义检验(验证class) 获取html加入 class <input id="D_NUMBER" name="D_NUMB ...

  6. highchat中的category编程object问题

    设置highchart时的category时我是动态赋值的形式category=cat;cat是['title','title']是X轴的坐标显示但当单击chat的图例时X轴变成了object: ca ...

  7. Block作为property属性实现页面之间传值(代替Delegate代理与协议结合的方法)

    需求:在ViewController中,点击Button,push到下一个页面NextViewController,在NextViewController的输入框TextField中输入一串字符,返回 ...

  8. css 清除浮动(转)

    转自http://hi.baidu.com/kongcheng2012/item/2b1250d4452e802538f6f705 为什么浮动这么难? 因为浮动会使当前标签产生向上浮的效果,同时会影响 ...

  9. ubuntu12.04 安装 setuptools

    ubuntu 12.04 安装django时,提示缺少setuptools. 转载自: http://blog.csdn.net/xudongtiankong/article/details/8180 ...

  10. 为 UIButton 添加长按事件

    UIButton *aBtn=[UIButtonbuttonWithType:UIButtonTypeCustom]; [aBtn setFrame:CGRectMake(40, 100, 60, 6 ...