• C#客户端绑定DataView和DataTable的几个技巧


    这几个问题算不上高级技巧,但是的确还有很多人不知道,尤其是对DataView了解比较少,下面代码演示了如何绑定数据到combobx和datagridview,另外还包括了如何在绑定时过滤重复,设置联动,以及如何利用DataRelation求汇总表格。程序界面请读者自己生成,一个form,两个combobox,三个datagridview就可以了。

    参考界面如下

    程序代码如下

    using  System;
    using  System.ComponentModel;
    using  System.Data;
    using  System.Windows.Forms;

    namespace  WindowsApplication6
    {
        
    public   partial   class  Form1 : Form
        
    {
            
    public  Form1()
            
    {
                InitializeComponent();
                initData();
            }


            System.Data.DataTable dt 
    =   new  DataTable();
            System.Data.DataSet ds 
    =   new  DataSet();

            
    ///   <summary>
            
    ///  初始化数据
            
    ///   </summary>

             private   void  initData()
            
    {
                dt.TableName 
    =   " Table1 " ;
                ds.Tables.Add(dt);
                dt.Columns.Add(
    " ID " typeof ( string ));
                dt.Columns.Add(
    " 板块 " typeof ( string ));
                dt.Columns.Add(
    " 积分 " typeof ( int ));

                addData(dt, 
    " jinjazz " " delphi " 50000 );
                addData(dt, 
    " jinjazz " " Sqlserver " 10000 );
                addData(dt, 
    " jinjazz " " .net " 20000 );
                addData(dt, 
    " zjcxc " " Sqlserver " 900000 );
                addData(dt, 
    " zjcxc " " vb " 10000 );
                addData(dt, 
    " zswang " " delphi " 70000 );
                addData(dt, 
    " zswang " " .net " 30000 );
            }


            
    private   void  addData(DataTable dt,  string  Name,  string  Catalog,  int  Salary)
            
    {
                System.Data.DataRow drow 
    =  dt.NewRow();
                drow[
    " ID " =  Name;
                drow[
    " 板块 " =  Catalog;
                drow[
    " 积分 " =  Salary;
                dt.Rows.Add(drow);
            }



            
    private   void  Form1_Load( object  sender, EventArgs e)
            
    {

                
    // 绑定所有ID到comboBox1
                 this .comboBox1.DataSource  =  dt.DefaultView.ToTable( true new   string []  " ID "  } );
                
    this .comboBox1.DisplayMember  =   " ID " ;

                
    // 绑定所有板块到comboBox2
                 this .comboBox2.DataSource  =   new  DataView(dt);
                
    this .comboBox2.DisplayMember  =   " 板块 " ;

                
    // 设置两个联动刷新
                 this .comboBox1.SelectedIndexChanged  +=   new  EventHandler(comboBox1_SelectedIndexChanged);

                
    // 绑定所有数据到dataGridView1
                 this .dataGridView1.DataSource  =  dt;

                
    // 绑定所有数据到dataGridView2
                 this .dataGridView2.DataSource  =   new  DataView(dt);

                
    // 利用DataRelation求汇总表格
                System.Data.DataTable dtGroup1 = dt.DefaultView.ToTable( true , " ID " );
                dtGroup1.TableName 
    =   " Table2 " ;
                ds.Tables.Add(dtGroup1);
                System.Data.DataRelation dr 
    =   new  DataRelation( " relation " , dtGroup1.Columns[ " ID " ], dt.Columns[ " ID " ]);
                ds.Relations.Add(dr);
                dtGroup1.Columns.Add(
    " 总积分 " ).Expression  =   " sum(child(relation).积分) " ;
                dtGroup1.Columns.Add(
    " 板块数 " ).Expression  =   " count(child(relation).板块) " ;
                
    this .dataGridView3.DataSource  =  dtGroup1;
     
            }


            
    void  comboBox1_SelectedIndexChanged( object  sender, EventArgs e)
            
    {
                
    // 设置combobox2和comboBox1同步
                System.Data.DataView dv  =   this .comboBox2.DataSource  as  DataView;
                dv.RowFilter 
    =   string .Format( " ID='{0}' " this .comboBox1.Text);
                
    // 设置dataGridView2和comboBox1同步
                dv  =   this .dataGridView2.DataSource  as  DataView;
                dv.RowFilter 
    =   string .Format( " ID='{0}' " this .comboBox1.Text);

            }
             
        }

    }
  • 相关阅读:
    python小练习
    python学习笔记
    google测试之道读书笔记一
    webservice头部认证
    我们需要什么样的测试?
    pt-ioprofile在CentOS7上无法运行的解决办法
    推荐Nginx系列文章
    linux开启coredump的3种方法
    55, select/poll returned error
    centos ftp安装
  • 原文地址:https://www.cnblogs.com/cl1024cl/p/6204950.html
Copyright © 2020-2023  润新知