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


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace BindDataGridViewAndDataTable
    {
        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);

                  this.comboBox2.SelectedIndexChanged += new EventHandler(comboBox2_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 comboBox2_SelectedIndexChanged(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
               
            }


           private 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);

            }


        }
    }

  • 相关阅读:
    C# 数据库访问
    ArcGIS API For Silverlight使用在线地图的多种方法总结
    Google Map Api 谷歌地图接口整理
    定义函数up(ch),如字符变量ch是小写字母就转换成大写字母并通过up返回,否则字符ch不改变。要求在短小而完全的程序中显示这个程序是怎样被调用的-简单
    编写一个求方程ax^2+bx+c=0的根的程序,用3个函数分别求当b^2-4ac大于零、等于零和小于零时的方程的根。要求从主函数输入a、b、c的值并输出结果-简单
    已知一个string的对象str的内容为“we are here!”, 使用多种方法输出字符“h”-简单
    使用多种方法编写将两个字符串连接在一起的程序-简单
    c++简单函数应用
    编写一个完整的程序,它读入15个float值,用指针把它们存放在一个存储快里,然后输出这些值的和以及最小值
    编写一个为int型变量分配100个整型量空间的程序-简单
  • 原文地址:https://www.cnblogs.com/beeone/p/2005328.html
Copyright © 2020-2023  润新知