datagridview實現(xiàn)手動添加行數(shù)據(jù)
datagridview手動添加行數(shù)據(jù)
我在做軟件模型界面時,通過功能按鈕觸發(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;
這樣就會自動產(chǎn)生對應(yīng)數(shù)據(jù)的行數(shù)據(jù)了。如果不自動產(chǎn)生列,則需手動添加列,把列的數(shù)據(jù)源屬性名(DataPropertyName)設(shè)置為對應(yīng)數(shù)據(jù)類的屬性名。
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";
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#程序連接數(shù)據(jù)庫及讀取數(shù)據(jù)庫中字段的簡單方法總結(jié)
包括C#連接Access、Oracle或者SQL Server,這里整理了一些C#連接數(shù)據(jù)庫及從讀取數(shù)據(jù)庫中字段的簡單方法總結(jié),需要的朋友可以參考下2016-05-05C#采用mouse_event函數(shù)實現(xiàn)模擬鼠標(biāo)功能
這篇文章主要介紹了C#模擬鼠標(biāo)點擊小功能,通過代碼向大家做分析,需要的朋友可以參考下2015-07-07C#3.0使用EventLog類寫Windows事件日志的方法
這篇文章主要介紹了C#3.0使用EventLog類寫Windows事件日志的方法,以簡單實例形式分析了C#寫windows事件日志的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08C#使用動態(tài)規(guī)劃解決0-1背包問題實例分析
這篇文章主要介紹了C#使用動態(tài)規(guī)劃解決0-1背包問題,實例分析了C#動態(tài)規(guī)劃算法的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04C#實現(xiàn)強制關(guān)閉當(dāng)前程序進程
這篇文章主要介紹了C#實現(xiàn)強制關(guān)閉當(dāng)前程序進程,本文直接給出實現(xiàn)代碼,可以實現(xiàn)完全Kill掉不留痕跡,需要的朋友可以參考下2015-06-06