程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#客戶端綁定DataView和DataTable的幾個技巧

C#客戶端綁定DataView和DataTable的幾個技巧

編輯:關於C#

這幾個問題算不上高級技巧,但是的確還有很多人不知道,尤其是對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);

    }
  }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved