日期:2014-05-17  浏览次数:20908 次

C#.winform 用DataTable把列添进Datagridview

用DataTable把列添进DataGridview 放在Load 事件里面

DataTable dtVoucher = new DataTable();
dtVoucher.Columns.Add("acCode", typeof(string));
            dtVoucher.Columns.Add("acName", typeof(string));
            dtVoucher.Columns.Add("particulars", typeof(string));
            dtVoucher.Columns.Add("debit", typeof(string));
            dtVoucher.Columns.Add("credit", typeof(string));
            DataRow dr_one = dtVoucher.NewRow();
            dr_one["acCode"] = "";
            dr_one["acName"] = "";
            dr_one["particulars"] = "";
            dr_one["debit"] = "";
            dr_one["credit"] = "";
            dtVoucher.Rows.Add(dr_one);
            DataRow dr_two = dtVoucher.NewRow();
            dr_two["acCode"] = "";
            dr_two["acName"] = "";
            dr_two["particulars"] = "";
            dr_two["debit"] = "";
            dr_two["credit"] = "";

            dtVoucher.Rows.Add(dr_two);
            dataGridView1.DataSource = dtVoucher;

?