csstome 发表于 2013-1-26 14:19:06

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

这几个问题算不上高级技巧,但是的确还有很多人不知道,尤其是对DataView了解比较少,下面代码演示了如何绑定数据到combobx和datagridview,另外还包括了如何在绑定时过滤重复,设置联动,以及如何利用DataRelation求汇总表格。程序界面请读者自己生成,一个form,两个combobox,三个datagridview就可以了。
参考界面如下
http://p.blog.csdn.net/images/p_blog_csdn_net/jinjazz/355056/o_data.JPG
程序代码如下
<div style="padding: 4px 5.4pt; width: 95%;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.ComponentModel;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Data;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Windows.Forms;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifnamespaceWindowsApplication6
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicpartialclassForm1:Form
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicForm1()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifInitializeComponent();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifinitData();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSystem.Data.DataTabledt=newDataTable();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSystem.Data.DataSetds=newDataSet();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif/**////<summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif///初始化数据
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif///</summary>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprivatevoidinitData()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdt.TableName="Table1";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifds.Tables.Add(dt);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdt.Columns.Add("ID",typeof(string));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdt.Columns.Add("板块",typeof(string));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdt.Columns.Add("积分",typeof(int));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifaddData(dt,"jinjazz","delphi",50000);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifaddData(dt,"jinjazz","Sqlserver",10000);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifaddData(dt,"jinjazz",".net",20000);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifaddData(dt,"zjcxc","Sqlserver",900000);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifaddData(dt,"zjcxc","vb",10000);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifaddData(dt,"zswang","delphi",70000);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifaddData(dt,"zswang",".net",30000);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprivatevoidaddData(DataTabledt,stringName,stringCatalog,intSalary)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSystem.Data.DataRowdrow=dt.NewRow();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdrow["ID"]=Name;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdrow["板块"]=Catalog;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdrow["积分"]=Salary;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdt.Rows.Add(drow);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprivatevoidForm1_Load(objectsender,EventArgse)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//绑定所有ID到comboBox1
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gifthis.comboBox1.DataSource=dt.DefaultView.ToTable(true,newstring[]...{"ID"});
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.comboBox1.DisplayMember="ID";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//绑定所有板块到comboBox2
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.comboBox2.DataSource=newDataView(dt);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.comboBox2.DisplayMember="板块";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//设置两个联动刷新
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.comboBox1.SelectedIndexChanged+=newEventHandler(comboBox1_SelectedIndexChanged);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//绑定所有数据到dataGridView1
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.dataGridView1.DataSource=dt;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//绑定所有数据到dataGridView2
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.dataGridView2.DataSource=newDataView(dt);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//利用DataRelation求汇总表格
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSystem.Data.DataTabledtGroup1=dt.DefaultView.ToTable(true,"ID");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdtGroup1.TableName="Table2";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifds.Tables.Add(dtGroup1);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSystem.Data.DataRelationdr=newDataRelation("relation",dtGroup1.Columns["ID"],dt.Columns["ID"]);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifds.Relations.Add(dr);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdtGroup1.Columns.Add("总积分").Expression="sum(child(relation).积分)";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdtGroup1.Columns.Add("板块数").Expression="count(child(relation).板块)";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.dataGridView3.DataSource=dtGroup1;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifvoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//设置combobox2和comboBox1同步
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSystem.Data.DataViewdv=this.comboBox2.DataSourceasDataView;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdv.RowFilter=string.Format("ID='{0}'",this.comboBox1.Text);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//设置dataGridView2和comboBox1同步
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdv=this.dataGridView2.DataSourceasDataView;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdv.RowFilter=string.Format("ID='{0}'",this.comboBox1.Text);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
页: [1]
查看完整版本: C#客户端绑定DataView和DataTable的几个技巧