日期:2014-05-18 浏览次数:20812 次
public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); db(); } public void db() { DataTable dt = new DataTable(); DataColumn[] td = new DataColumn[4]; td[0] = new DataColumn("name", typeof(string)); td[1] = new DataColumn("age", typeof(Int32)); td[2] = new DataColumn("je", typeof(Int32)); td[3] = new DataColumn("dz", typeof(string)); dt.Columns.AddRange(td); DataRow tr = null; for (int i = 0; i < 3; i++) { tr = dt.NewRow(); tr[0] = "name"; tr[1] = 31; tr[2] = 20; tr[3] = "江西"; dt.Rows.Add(tr); tr = dt.NewRow(); tr[0] = "name"; tr[1] = 30; tr[2] = 20; tr[3] = "江西"; dt.Rows.Add(tr); tr = dt.NewRow(); tr[0] = "name1"; tr[1] = 20; tr[2] = 10; tr[3] = "浙江"; dt.Rows.Add(tr); tr = dt.NewRow(); tr[0] = "name1"; tr[1] = 21; tr[2] = 10; tr[3] = "浙江"; dt.Rows.Add(tr); tr = dt.NewRow(); tr[0] = "name2"; tr[1] = 10; tr[2] = 5; tr[3] = "湖北"; dt.Rows.Add(tr); } dataGridView1.DataSource = dt; } public object DataSource { get { return dataGridView1.DataSource; } set { dataGridView1.DataSource = value; } } }