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

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

datagridview實(shí)現(xiàn)手動(dòng)添加行數(shù)據(jù)_C#教程

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

datagridview手動(dòng)添加行數(shù)據(jù)

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

代碼如下:

? ? ? ? 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、數(shù)據(jù)綁定

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

這樣就會(huì)自動(dòng)產(chǎn)生對應(yīng)數(shù)據(jù)的行數(shù)據(jù)了。如果不自動(dòng)產(chǎn)生列,則需手動(dòng)添加列,把列的數(shù)據(jù)源屬性名(DataPropertyName)設(shè)置為對應(yīng)數(shù)據(jù)類的屬性名。

2、手動(dòng)添加

? ?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";
//或者,等價(jià)的
this.dataGridView1[0, 1].Value = "new value";

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

欄目分類
最近更新