zoukankan      html  css  js  c++  java
  • 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);

            }
             
        }

    }
  • 相关阅读:
    zoj 3593 One Person Game
    poj 2115 C Looooops
    hdu 1576 A/B
    hdu 2669 Romantic
    poj1006 Biorhythms
    中国剩余定理(孙子定理)
    Pseudoprime numbers---费马小定理
    青蛙的约会----POJ1061
    [POJ2942]:Knights of the Round Table(塔尖+二分图染色法)
    [BZOJ1718]:[Usaco2006 Jan] Redundant Paths 分离的路径(塔尖)
  • 原文地址:https://www.cnblogs.com/cl1024cl/p/6204950.html
Copyright © 2011-2022 走看看