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

WinForm之BindingSource基础操作实例教程

程序员文章站 2023-12-21 14:40:52
通常我们在进行数据绑定的时候,常用的数据源有dataset、datatable、bindinglist、还有强类型数据源。今天我们来通过实例了解一下bin...

通常我们在进行数据绑定的时候,常用的数据源有dataset、datatable、bindinglist<t>、还有强类型数据源。今天我们来通过实例了解一下bindingsource组建,分享给大家供大家参考借鉴之用。

bindingsource的两个用途:

(1)首先,它提供一个将窗体上的控件绑定到数据的间接层。这是通过将 bindingsource 组件绑定到数据源,然后将窗体上的控件绑定到 bindingsource 组件来完成的。与数据的所有进一步交互(包括导航、排序、筛选和更新)都是通过调用 bindingsource 组件来完成的。
(2)其次,bindingsource 组件可以充当强类型数据源。使用 add 方法向 bindingsource 组件添加类型会创建一个该类型的列表。

一、对bindingsource的基础操作——增删改查

实例代码如下:

  public partial class form1 : form
  {
    //注当前dgv已经绑定到 id 和 name 列
    private bindingsource source = new bindingsource();
    public form1()
    {
      initializecomponent();
    }
    //窗体加载
    private void form1_load(object sender, eventargs e)
    {
      this.source.datasource = typeof(custom);
      this.datagridview1.datasource = this.source;
    }
    //添加
    private void button1_click(object sender, eventargs e)
    {
      this.source.add(new custom(1,"a"));
      this.source.add(new custom(2,"b"));
    }
    //删除
    private void button2_click(object sender, eventargs e)
    {
      this.source.removeat(0);
    }
    //排序 【有问题】
    private void button3_click(object sender, eventargs e)
    {
      this.source.sort = "id asc";
      this.source.resetbindings(false);
    }
    //筛选 【有问题】
    private void button4_click(object sender, eventargs e)
    {
      this.source.filter = "id = 1";
      this.source.resetbindings(false);
    }
    //向下移动
    private void button5_click(object sender, eventargs e)
    {
      this.source.movenext();
      messagebox.show(this.source.position.tostring());
    }
    //向上移动
    private void button9_click(object sender, eventargs e)
    {
      this.source.moveprevious();
      messagebox.show(this.source.position.tostring());
    }
    //获取当前项
    private void button6_click(object sender, eventargs e)
    {
      custom custom = (custom)this.source.current;
      messagebox.show(" 所处的位置 : " + this.source.indexof(custom).tostring());
      messagebox.show("custom.name : " + custom.name);
    }
    //修改当前项
    private void button7_click(object sender, eventargs e)
    {
      custom custom = (custom)this.source.current;
      custom.name = "修改后的值";
      this.source.resetcurrentitem();
    }
    //删除当前项
    private void button8_click(object sender, eventargs e)
    {
      custom custom = (custom)this.source.current;
      this.source.remove(custom);
    }
  }
  //自定义类 字段必须属性公开化
  public class custom
  {
    public custom()
    { }
    public custom(int id, string name)
    {
      this.id = id;
      this.name = name;
    }
    private int id;
    public int id
    {
      get { return id; }
      set { id = value; }
    }
    private string name;
    public string name
    {
      get { return name; }
      set { name = value; }
    }
  }

二、  下面的示例演示如何在两种不同情况下绑定 dbnull 值。

第一种情况演示如何设置字符串属性的 nullvalue;第二种情况演示如何设置图像属性的 nullvalue。

下面的示例演示如何在两种不同情况下绑定 dbnull 值。第一种情况演示如何设置字符串属性的 nullvalue;第二种情况演示如何设置图像属性的 nullvalue。

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.data.sqlclient;
using system.windows.forms;
namespace dbnullcs
{
  public class form1 : form
  {
    public form1()
    {    
      this.load += new eventhandler(form1_load);
    }
    // the controls and components we need for the form.
    private button button1;
    private picturebox picturebox1;
    private bindingsource bindingsource1;
    private textbox textbox1;
    private textbox textbox2;
    // data table to hold the database data.
    datatable employeetable = new datatable();
    void form1_load(object sender, eventargs e)
    {
      // basic form setup.
      this.picturebox1 = new picturebox();
      this.bindingsource1 = new bindingsource();
      this.textbox1 = new textbox();
      this.textbox2 = new textbox();
      this.button1 = new button();
      this.picturebox1.location = new system.drawing.point(20, 20);
      this.picturebox1.size = new system.drawing.size(174, 179);
      this.textbox1.location = new system.drawing.point(25, 215);
      this.textbox1.readonly = true;
      this.textbox2.location = new system.drawing.point(25, 241);
      this.textbox2.readonly = true;
      this.button1.location = new system.drawing.point(200, 103);
      this.button1.text = "move next";
      this.button1.click += new system.eventhandler(this.button1_click);
      this.clientsize = new system.drawing.size(292, 273);
      this.controls.add(this.button1);
      this.controls.add(this.textbox2);
      this.controls.add(this.textbox1);
      this.controls.add(this.picturebox1);
      this.resumelayout(false);
      this.performlayout();
      // create the connection string and populate the data table
      // with data.
      string connectionstring = "integrated security=sspi;" +
        "persist security info = false;initial catalog=northwind;" +
        "data source = localhost";
      sqlconnection connection = new sqlconnection();
      connection.connectionstring = connectionstring;
      sqldataadapter employeeadapter = 
        new sqldataadapter(new sqlcommand("select * from employees", connection));
      connection.open();
      employeeadapter.fill(employeetable);
      // set the datasource property of the bindingsource to the employee table.
      bindingsource1.datasource = employeetable;
      // set up the binding to the reportsto column.
      binding reportstobinding = textbox2.databindings.add("text", bindingsource1, 
        "reportsto", true);
      // set the nullvalue property for this binding.
      reportstobinding.nullvalue = "no manager";
      // set up the binding for the picturebox using the add method, setting
      // the null value in method call.
      picturebox1.databindings.add("image", bindingsource1, "photo", true, 
        datasourceupdatemode.never, new bitmap(typeof(button), "button.bmp"));
      // set up the remaining binding.
      textbox1.databindings.add("text", bindingsource1, "lastname", true);
    }
    // move through the data when the button is clicked.
    private void button1_click(object sender, eventargs e)
    {
      bindingsource1.movenext();
    }
    [stathread]
    static void main()
    {
      application.enablevisualstyles();
      application.run(new form1());
    }
  }
}

希望本文实例对大家c#程序设计的学习有所帮助!

上一篇:

下一篇: