日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

datagridview實現手動添加行數據_C#教程

作者:simpleshao ? 更新時間: 2022-06-18 編程語言

datagridview手動添加行數據

我在做軟件模型界面時,通過功能按鈕觸發顯示的datagridview中,為了方便,需要一些數據,僅寫死數據就可以了,因此,不需要連接數據表,直接添加行就可以了。

代碼如下:

? ? ? ? int index = this.dataGridView1.Rows.Add();
? ? ? ? this.dataGridView1.Rows[index].Cells[0].Value = "1";
? ? ? ? this.dataGridView1.Rows[index].Cells[1].Value = "11";
? ? ? ? this.dataGridView1.Rows[index].Cells[2].Value = "1111";
? ? ? ? this.dataGridView1.Rows[index].Cells[3].Value = "11111";
? ? ? ? this.dataGridView1.Rows[index].Cells[4].Value = "111111";
? ? ? ? this.dataGridView1.Rows[index].Cells[5].Value = "1111111";
? ? ? ? this.dataGridView1.Rows[index].Cells[6].Value = "*-*";

datagridview添加行的幾種方式

1、數據綁定

dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = customersDataSet;

這樣就會自動產生對應數據的行數據了。如果不自動產生列,則需手動添加列,把列的數據源屬性名(DataPropertyName)設置為對應數據類的屬性名。

2、手動添加

? ?songsDataGridView.ColumnCount = 5;
? ?....
? ?songsDataGridView.Columns[0].Name = "Release Date";
? ?....

string[] row0 = { "11/22/1968", "29", "Revolution 9",?
"Beatles", "The Beatles [White Album]" };
songsDataGridView.Rows.Add(row0);

另外,訪問行單元格的方式可以這樣

this.dataGridView1.Rows[1].Cells[0].Value = "new value";
//或者,等價的
this.dataGridView1[0, 1].Value = "new value";

原文鏈接:https://blog.csdn.net/simpleshao/article/details/78642589

欄目分類
最近更新