欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中DataBindings用法实例分析

程序员文章站 2022-05-18 14:17:38
本文实例讲述了c#中databindings用法。分享给大家供大家参考,具体如下: 在c#操作数据库过程中,针对一般的文本控件,比如textbox,label等,我们赋值...

本文实例讲述了c#中databindings用法。分享给大家供大家参考,具体如下:

在c#操作数据库过程中,针对一般的文本控件,比如textbox,label等,我们赋值直接使用类似textbox.text=****的方式来进行,这种方式从某种意义上来说的确是最简便的方式,但是对于复杂一些的空间,比如说datagridview,这个时候,绑定数据源我们一般使用datagridview1.datasource=****的方式来进行,如果数据源稍微有更改,那么只需要重新调用绑定一遍即可。可以说这种方式是单向的,也即从数据库到ui,但是有没有一种方式能够实现数据源改变的时候,不用重新绑定datagridview就让它能够自动刷新数据呢,当然,这里要提到的就是databinding了。

代码如下

form2.cs代码:

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;
namespace databindingstest
{
  public partial class form2 : form
  {
    public form2()
    {
      initializecomponent();
    }
    mydatasource mydatasource = new mydatasource(); //应用于第二种方式
    public int num { get; set; } //应用于第三种方式
    public list<blognew> blognews { get; set; } //应用于第四种方式
    public bindinglist<blognew> blognewsregardui { get; set; } //应用于datagridview界面ui更新 
    private void mainfrm_load(object sender, eventargs e)
    {
      #region 测试一
      /************************************************
       * 第一个值:要绑定到textbox的什么地方
       * 第二个值:数据源是什么
       * 第三个值:应该取数据源的什么属性
       * 第四个值:是否开启数据格式化
       * 第五个值:在什么时候启用数据源绑定
       * *********************************************/
      textbox1.databindings.add("text", trackbar1, "value", false, datasourceupdatemode.onpropertychanged);
      #endregion
      #region 测试二
      /*********************************************
       * 这个主要就是通过一个外部的类,当做数据源
       * *********************************************/
      mydatasource.myvalue = "这是个测试";
      textbox2.databindings.add("text", mydatasource, "myvalue", false, datasourceupdatemode.onpropertychanged);
      #endregion
      #region 测试三
      /*****************************************
       *这个主要就是通过本身拥有的属性,当做数据源
       ****************************************/
      num = 5;
      textbox3.databindings.add("text", this, "num", false, datasourceupdatemode.onpropertychanged);
      #endregion
      /*
       * 注意:上面的3个测试,改变文本框中的值,数据源中对应的属性值会改变  
       *    但是,数据源的属性值改变了,文本框中的值不会改变
       */
      #region 测试四 : list<t>
      blognews = new list<blognew>();
      blognews.add(new blognew { blogid = 1, blogtitle = "人生若只如初见" });
      blognews.add(new blognew { blogid = 2, blogtitle = "何事秋风悲画扇" });
      blognews.add(new blognew { blogid = 3, blogtitle = "最喜欢纳兰性德" });
      datagridview1.databindings.add("datasource", this, "blognews", false, datasourceupdatemode.onpropertychanged);
      #endregion
      #region 测试五 : bindinglist<t>
      blognewsregardui = new bindinglist<blognew>();
      blognewsregardui.add(new blognew { blogid = 11, blogtitle = "僵卧孤村不自哀" });
      blognewsregardui.add(new blognew { blogid = 12, blogtitle = "尚思为国戍轮台" });
      blognewsregardui.add(new blognew { blogid = 13, blogtitle = "夜阑卧听风吹雨" });
      datagridview2.databindings.add("datasource", this, "blognewsregardui", false, datasourceupdatemode.onpropertychanged);
      #endregion
    }
    private void button1_click(object sender, eventargs e)
    {
      //从这里可以看出,改变了textbox2中的值,这里的值也改变了,原因是因为类属于引用类型
      messagebox.show(mydatasource.myvalue);
    }
    private void button2_click(object sender, eventargs e)
    {
      //从这里可以看出,改变了textbox3中的值,这里的值也改变了,
      //原因是num被当做了当前窗体的一个属性(窗体本身就是一个类),也属于引用类型
      messagebox.show(num.tostring());
      //this.num = 10;
      //messagebox.show(num.tostring());
    }
    private void button3_click(object sender, eventargs e)
    {
      //在这里向datagridview中插入一行
      var data = datagridview1.datasource as list<blognew>;
      data.add(new blognew { blogid = 4, blogtitle = "取次花丛懒回顾,半缘修道半缘君" });
      foreach (blognew blognew in datagridview1.datasource as list<blognew>)
      {
        /***********
         * 当我们心插入一条blogid记录为4的数据的时候,在界面上可以看出datagridview1的datasource已经被更新,
         * 但是界面上依旧显示为blogid为1,2,3三条数据,很奇怪
         * *********************/
        messagebox.show(blognew.blogid + "--" + blognew.blogtitle);
      }
    }
    private void button4_click(object sender, eventargs e)
    {
      /*这里主要用来解决datagridview1界面不更新的问题,其实原因在于使用了list<blognew>,这里我们采用bindlist<blognew>
       *通过测试,我们发现,只要数据源改变,界面就可以自动的进行更新了,很是方便,不需要重新绑定
       */
      var dataregardui = datagridview2.datasource as bindinglist<blognew>;
      dataregardui.add(new blognew { blogid = 20, blogtitle = "竹外桃花三两枝,春江水暖鸭先知" });
    }
  }
  public class mydatasource
  {
    public string myvalue { get; set; }
  }
  public class blognew
  {
    public int blogid { get; set; }
    public string blogtitle { get; set; }
  }
}

form2.designer.cs代码:

namespace databindingstest
{
  partial class form2
  {
    /// <summary>
    /// required designer variable.
    /// </summary>
    private system.componentmodel.icontainer components = null;
    /// <summary>
    /// clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void dispose(bool disposing)
    {
      if (disposing && (components != null))
      {
        components.dispose();
      }
      base.dispose(disposing);
    }
    #region windows form designer generated code
    /// <summary>
    /// required method for designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void initializecomponent()
    {
      this.groupbox1 = new system.windows.forms.groupbox();
      this.groupbox2 = new system.windows.forms.groupbox();
      this.button1 = new system.windows.forms.button();
      this.textbox2 = new system.windows.forms.textbox();
      this.groupbox3 = new system.windows.forms.groupbox();
      this.button2 = new system.windows.forms.button();
      this.textbox3 = new system.windows.forms.textbox();
      this.groupbox4 = new system.windows.forms.groupbox();
      this.button3 = new system.windows.forms.button();
      this.datagridview1 = new system.windows.forms.datagridview();
      this.groupbox5 = new system.windows.forms.groupbox();
      this.button4 = new system.windows.forms.button();
      this.datagridview2 = new system.windows.forms.datagridview();
      this.textbox1 = new system.windows.forms.textbox();
      this.trackbar1 = new system.windows.forms.trackbar();
      this.groupbox1.suspendlayout();
      this.groupbox2.suspendlayout();
      this.groupbox3.suspendlayout();
      this.groupbox4.suspendlayout();
      ((system.componentmodel.isupportinitialize)(this.datagridview1)).begininit();
      this.groupbox5.suspendlayout();
      ((system.componentmodel.isupportinitialize)(this.datagridview2)).begininit();
      ((system.componentmodel.isupportinitialize)(this.trackbar1)).begininit();
      this.suspendlayout();
      // 
      // groupbox1
      // 
      this.groupbox1.controls.add(this.trackbar1);
      this.groupbox1.controls.add(this.textbox1);
      this.groupbox1.location = new system.drawing.point(12, 12);
      this.groupbox1.name = "groupbox1";
      this.groupbox1.size = new system.drawing.size(200, 100);
      this.groupbox1.tabindex = 0;
      this.groupbox1.tabstop = false;
      this.groupbox1.text = "方式一";
      // 
      // groupbox2
      // 
      this.groupbox2.controls.add(this.button1);
      this.groupbox2.controls.add(this.textbox2);
      this.groupbox2.location = new system.drawing.point(218, 12);
      this.groupbox2.name = "groupbox2";
      this.groupbox2.size = new system.drawing.size(200, 100);
      this.groupbox2.tabindex = 2;
      this.groupbox2.tabstop = false;
      this.groupbox2.text = "方式二";
      // 
      // button1
      // 
      this.button1.location = new system.drawing.point(22, 59);
      this.button1.name = "button1";
      this.button1.size = new system.drawing.size(157, 23);
      this.button1.tabindex = 3;
      this.button1.text = "查看已修改的数据源的值";
      this.button1.usevisualstylebackcolor = true;
      this.button1.click += new system.eventhandler(this.button1_click);
      // 
      // textbox2
      // 
      this.textbox2.location = new system.drawing.point(22, 20);
      this.textbox2.name = "textbox2";
      this.textbox2.size = new system.drawing.size(157, 21);
      this.textbox2.tabindex = 2;
      // 
      // groupbox3
      // 
      this.groupbox3.controls.add(this.button2);
      this.groupbox3.controls.add(this.textbox3);
      this.groupbox3.location = new system.drawing.point(428, 12);
      this.groupbox3.name = "groupbox3";
      this.groupbox3.size = new system.drawing.size(200, 100);
      this.groupbox3.tabindex = 4;
      this.groupbox3.tabstop = false;
      this.groupbox3.text = "方式三";
      // 
      // button2
      // 
      this.button2.location = new system.drawing.point(22, 59);
      this.button2.name = "button2";
      this.button2.size = new system.drawing.size(157, 23);
      this.button2.tabindex = 3;
      this.button2.text = "查看已修改的数据源的值";
      this.button2.usevisualstylebackcolor = true;
      this.button2.click += new system.eventhandler(this.button2_click);
      // 
      // textbox3
      // 
      this.textbox3.location = new system.drawing.point(22, 20);
      this.textbox3.name = "textbox3";
      this.textbox3.size = new system.drawing.size(157, 21);
      this.textbox3.tabindex = 2;
      // 
      // groupbox4
      // 
      this.groupbox4.controls.add(this.button3);
      this.groupbox4.controls.add(this.datagridview1);
      this.groupbox4.location = new system.drawing.point(12, 118);
      this.groupbox4.name = "groupbox4";
      this.groupbox4.size = new system.drawing.size(568, 157);
      this.groupbox4.tabindex = 5;
      this.groupbox4.tabstop = false;
      this.groupbox4.text = "方式四";
      // 
      // button3
      // 
      this.button3.location = new system.drawing.point(377, 122);
      this.button3.name = "button3";
      this.button3.size = new system.drawing.size(157, 23);
      this.button3.tabindex = 4;
      this.button3.text = "插入一行";
      this.button3.usevisualstylebackcolor = true;
      this.button3.click += new system.eventhandler(this.button3_click);
      // 
      // datagridview1
      // 
      this.datagridview1.columnheadersheightsizemode = system.windows.forms.datagridviewcolumnheadersheightsizemode.autosize;
      this.datagridview1.location = new system.drawing.point(18, 20);
      this.datagridview1.name = "datagridview1";
      this.datagridview1.rowtemplate.height = 23;
      this.datagridview1.size = new system.drawing.size(516, 96);
      this.datagridview1.tabindex = 0;
      // 
      // groupbox5
      // 
      this.groupbox5.controls.add(this.button4);
      this.groupbox5.controls.add(this.datagridview2);
      this.groupbox5.location = new system.drawing.point(12, 281);
      this.groupbox5.name = "groupbox5";
      this.groupbox5.size = new system.drawing.size(568, 162);
      this.groupbox5.tabindex = 6;
      this.groupbox5.tabstop = false;
      this.groupbox5.text = "方式五";
      // 
      // button4
      // 
      this.button4.location = new system.drawing.point(377, 127);
      this.button4.name = "button4";
      this.button4.size = new system.drawing.size(157, 23);
      this.button4.tabindex = 4;
      this.button4.text = "插入一行";
      this.button4.usevisualstylebackcolor = true;
      this.button4.click += new system.eventhandler(this.button4_click);
      // 
      // datagridview2
      // 
      this.datagridview2.columnheadersheightsizemode = system.windows.forms.datagridviewcolumnheadersheightsizemode.autosize;
      this.datagridview2.location = new system.drawing.point(18, 20);
      this.datagridview2.name = "datagridview2";
      this.datagridview2.rowtemplate.height = 23;
      this.datagridview2.size = new system.drawing.size(516, 91);
      this.datagridview2.tabindex = 0;
      // 
      // textbox1
      // 
      this.textbox1.location = new system.drawing.point(18, 20);
      this.textbox1.name = "textbox1";
      this.textbox1.size = new system.drawing.size(157, 21);
      this.textbox1.tabindex = 0;
      // 
      // trackbar1
      // 
      this.trackbar1.location = new system.drawing.point(18, 47);
      this.trackbar1.name = "trackbar1";
      this.trackbar1.size = new system.drawing.size(157, 45);
      this.trackbar1.tabindex = 1;
      // 
      // form2
      // 
      this.autoscaledimensions = new system.drawing.sizef(6f, 12f);
      this.autoscalemode = system.windows.forms.autoscalemode.font;
      this.clientsize = new system.drawing.size(676, 471);
      this.controls.add(this.groupbox5);
      this.controls.add(this.groupbox4);
      this.controls.add(this.groupbox3);
      this.controls.add(this.groupbox2);
      this.controls.add(this.groupbox1);
      this.name = "form2";
      this.text = "form2";
      this.load += new system.eventhandler(this.mainfrm_load);
      this.groupbox1.resumelayout(false);
      this.groupbox1.performlayout();
      this.groupbox2.resumelayout(false);
      this.groupbox2.performlayout();
      this.groupbox3.resumelayout(false);
      this.groupbox3.performlayout();
      this.groupbox4.resumelayout(false);
      ((system.componentmodel.isupportinitialize)(this.datagridview1)).endinit();
      this.groupbox5.resumelayout(false);
      ((system.componentmodel.isupportinitialize)(this.datagridview2)).endinit();
      ((system.componentmodel.isupportinitialize)(this.trackbar1)).endinit();
      this.resumelayout(false);
    }
    #endregion
    private system.windows.forms.groupbox groupbox1;
    private system.windows.forms.groupbox groupbox2;
    private system.windows.forms.button button1;
    private system.windows.forms.textbox textbox2;
    private system.windows.forms.groupbox groupbox3;
    private system.windows.forms.button button2;
    private system.windows.forms.textbox textbox3;
    private system.windows.forms.groupbox groupbox4;
    private system.windows.forms.button button3;
    private system.windows.forms.datagridview datagridview1;
    private system.windows.forms.groupbox groupbox5;
    private system.windows.forms.button button4;
    private system.windows.forms.datagridview datagridview2;
    private system.windows.forms.trackbar trackbar1;
    private system.windows.forms.textbox textbox1;
  }
}

效果图:

C#中DataBindings用法实例分析

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结

希望本文所述对大家c#程序设计有所帮助。