我有usercontrol
一个datagridview。我创建了一个数据表,并将数据网格的源设置为此数据表。我希望在运行时能够在每次单击按钮时在 gridview 上添加多少行。
我的代码:
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);
}