C# 如何显示表格
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);
}
}
}
效果如图
如果是WebForm的 你直接用DataGrid就好了,也可以用JS的控件,EasyUI, LigerUi什么的