![](/img/trans.png)
[英]How to add a new row to the datagridview and edit it in C# winform?
[英]How to add row in datagridview with pre-data in c# winform?
我有一個來自SQL的數據的datagridview。 我在每列下添加了2個數據,所以如果我以這種形式查看它,它將是這樣。
代碼是自動遞增的
Code -- Date -- Total_Score
1 1/3/2015 20
2 1/3/2015 30
現在我想添加一個新行..如果我單擊按鈕,它應該添加一個新行,其詳細信息為“ 3”(代碼從我的數據庫中自動遞增),1/3/2015(日期應為當前日期時間)和total_score單元格應該為空,這樣我就可以輸入數據了。 但是,它只是顯示一個新添加的空白行,我無法在上面輸入任何內容..這是我當前的代碼..
private void btnaddcomp_Click(object sender, EventArgs e)
{
DataTable ds = (DataTable)dgsetcompt.DataSource;
int index = this.dgsetcompt.Rows.Count;
DataRow dr = ds.NewRow();
ds.Rows.InsertAt(dr, index + 1);
dgsetcompt.DataSource = ds;
}
您應該以這種方式簡單地將數據添加到行中
private void btnaddcomp_Click(object sender, EventArgs e)
{
DataTable ds = (DataTable)dgsetcompt.DataSource;
// Create a new row...
DataRow dr = ds.NewRow();
// Set the two fields
dr["code"] = this.dgsetcompt.Rows.Count + 1;
dr["date"] = new DateTime(2015, 1, 3);
// Add the newly created row to the Rows collection of the datatable
ds.Rows.Add(dr);
// Not needed
//dgsetcompt.DataSource = ds;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.