datagridview實(shí)現(xiàn)手動(dòng)添加行數(shù)據(jù)
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)生對(duì)應(yīng)數(shù)據(jù)的行數(shù)據(jù)了。如果不自動(dòng)產(chǎn)生列,則需手動(dòng)添加列,把列的數(shù)據(jù)源屬性名(DataPropertyName)設(shè)置為對(duì)應(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";
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#程序連接數(shù)據(jù)庫(kù)及讀取數(shù)據(jù)庫(kù)中字段的簡(jiǎn)單方法總結(jié)
包括C#連接Access、Oracle或者SQL Server,這里整理了一些C#連接數(shù)據(jù)庫(kù)及從讀取數(shù)據(jù)庫(kù)中字段的簡(jiǎn)單方法總結(jié),需要的朋友可以參考下2016-05-05WPF實(shí)現(xiàn)調(diào)用本機(jī)攝像頭的示例代碼
這篇文章主要介紹了如何利用WPF實(shí)現(xiàn)調(diào)用本機(jī)攝像頭,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2022-08-08C#采用mouse_event函數(shù)實(shí)現(xiàn)模擬鼠標(biāo)功能
這篇文章主要介紹了C#模擬鼠標(biāo)點(diǎn)擊小功能,通過代碼向大家做分析,需要的朋友可以參考下2015-07-07C#導(dǎo)入導(dǎo)出EXCEL文件的代碼實(shí)例
這篇文章主要介紹了C#導(dǎo)入導(dǎo)出EXCEL文件代碼實(shí)例,代碼的流程和方法都很詳細(xì),需要的朋友可以參考下2014-04-04C#3.0使用EventLog類寫Windows事件日志的方法
這篇文章主要介紹了C#3.0使用EventLog類寫Windows事件日志的方法,以簡(jiǎn)單實(shí)例形式分析了C#寫windows事件日志的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08基于C#實(shí)現(xiàn)的多邊形沖突檢測(cè)實(shí)例
這篇文章主要給大家介紹了基于C#實(shí)現(xiàn)的多邊形沖突檢測(cè)的相關(guān)資料,文中介紹的方法并未使用第三方類庫(kù),可以完美解決這個(gè)問題,需要的朋友可以參考下2021-07-07C#使用動(dòng)態(tài)規(guī)劃解決0-1背包問題實(shí)例分析
這篇文章主要介紹了C#使用動(dòng)態(tài)規(guī)劃解決0-1背包問題,實(shí)例分析了C#動(dòng)態(tài)規(guī)劃算法的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04C#實(shí)現(xiàn)強(qiáng)制關(guān)閉當(dāng)前程序進(jìn)程
這篇文章主要介紹了C#實(shí)現(xiàn)強(qiáng)制關(guān)閉當(dāng)前程序進(jìn)程,本文直接給出實(shí)現(xiàn)代碼,可以實(shí)現(xiàn)完全Kill掉不留痕跡,需要的朋友可以參考下2015-06-06