10
A
回答
16
您可以通過以下方式訪問任何DGV細胞:
dataGridView1.Rows[rowIndex].Cells[columnIndex].Value = value;
但烏蘇盟友最好使用數據綁定:通過DataSource
屬性將DGV綁定到數據源(DataTable
,集合...),並且只能在數據源本身上工作。該DataGridView
會自動反映所做的更改,並在DataGridView
所做的更改將數據源
11
這是完美的代碼,但它不能添加新行上反映:
dataGridView1.Rows[rowIndex].Cells[columnIndex].Value = value;
但這種代碼可以插入一個新行:
this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[0].Cells[1].Value = "1";
this.dataGridView1.Rows[0].Cells[2].Value = "Baqar";
+0
我很確定索引在第二個示例中不固定。 – quantum 2012-10-27 01:48:55
+0
所以它不完美,因爲它不能添加新行? :) – 2016-04-01 03:26:19
0
,如果你想將數據添加到數據庫中,一個按鈕,您可以使用此功能。我希望這會有所幫助。
// dgvBill is name of DataGridView
string StrQuery;
try
{
using (SqlConnection conn = new SqlConnection(ConnectingString))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
conn.Open();
for (int i = 0; i < dgvBill.Rows.Count; i++)
{
StrQuery = @"INSERT INTO tblBillDetails (IdBill, productID, quantity, price, total) VALUES ('" + IdBillVar+ "','" + dgvBill.Rows[i].Cells[0].Value + "', '" + dgvBill.Rows[i].Cells[4].Value + "', '" + dgvBill.Rows[i].Cells[3].Value + "', '" + dgvBill.Rows[i].Cells[2].Value + "');";
comm.CommandText = StrQuery;
comm.ExecuteNonQuery();
}
}
}
}
catch (Exception err)
{
MessageBox.Show(err.Message , "Error !");
}
3
出於某種原因,我不能加號(字符串格式)到DataGridView 但是這個工作對我來說希望它能幫助別人!
//dataGridView1.Rows[RowCount].Cells[0].Value = FEString3;//This was not adding Stringed Numbers like "1","2","3"....
DataGridViewCell NewCell = new DataGridViewTextBoxCell();//Create New Cell
NewCell.Value = FEString3;//Set Cell Value
DataGridViewRow NewRow = new DataGridViewRow();//Create New Row
NewRow.Cells.Add(NewCell);//Add Cell to Row
dataGridView1.Rows.Add(NewRow);//Add Row To Datagrid
0
int index= datagridview.rows.add();
datagridview.rows[index].cells[1].value=1;
datagridview.rows[index].cells[2].value="a";
datagridview.rows[index].cells[3].value="b";
希望這有助於! :)
相關問題
- 1. 在數據庫中插入datagridview的值
- 2. C#插入值到DataGridView的
- 3. Datagridview cell forecolor question(winform)
- 4. 在DataGridView中插入行(C#)
- 5. datagridview中的值插入到datetimepicker
- 6. 如何允許在DataGridView上插入?
- 7. 如何在C#中使用MYSQL插入,刪除,選擇,更新datagridview中的值
- 8. 如何插入使用DataGridView - C#winforms?
- 9. DataGridView插入主鍵
- 10. 如何獲取JTable中Last cell的值?
- 11. 在DataGridView單元格中插入路徑
- 12. 如何在DataGridView中將「插入新行」設置爲第一行
- 13. 如何添加行並在datagridview中插入數據C#
- 14. 如何在按鈕上的DataGridView中插入列單擊?
- 15. 我如何在datagridview中模仿excel「插入註釋」
- 16. 如何在datagridview的單行多列中插入多行記錄
- 17. 如何在foreach中插入多個值
- 18. 如何在URL中插入值?
- 19. 如何在表中插入一個值?
- 20. 如何在Qt中插入空值?
- 21. 如何在SQLAlchemy中插入NULL值?
- 22. 如何在SQL中插入常量值
- 23. 如何插入值?
- 24. 如何基於DataGridView中包含的數據從DataSet中將值插入到DataGridView中?
- 25. 如何在大容量插入表中插入值?
- 26. 如何在codeigniter中插入值並插入數據庫?
- 27. 如何在mysql插入查詢中插入變量值?
- 28. 如何重新設置或刷新DataGridView Cell的背景顏色?
- 29. 如何將datagridview中的多個行值插入表的單個字段?
- 30. C#:如何調整用戶在DataGridView中輸入的值?
即使我們可以編輯和添加值並將其保存到數據庫 – SurajSing 2013-03-26 09:43:58