我有一個usercontrol
datagridview。我創建了一個數據表,並將datagrid的源設置爲這個數據表。我希望在運行時能夠在點擊每個按鈕時在gridview上添加多少行。在每個按鈕單擊添加行datagridview
我的代碼:
private DataTable CreateTable()
{
Datatable table=new Datatable();
table.Columns.Add("Name".ToString());
table.Columns.Add("Size".ToString());
DataRow dr = table.NewRow();
dr["Name"] = "Mike";
DataRow dr2 = table.NewRow();
dr2["Name"] = "Ryan;
DataRow dr3 = table.NewRow();
dr3["Name"] = "Taylor";
dr["Size"] = " one";
dr2["Size"] = "two";
table.Rows.Add(dr);
table.Rows.Add(dr2);
table.Rows.Add(dr3);
return table;
//and on my constructor I set gridview.DataSource=Datatable;
}
//Code on the event:
private void button_Click(object sender, EventArgs e)
{
DataRow NewRow = table.NewRow();
table.Rows.Add(NewRow);
}
非常感謝!它完美的工作! – Viva
@Mady,歡迎您:) – Habib