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)生對應數(shù)據(jù)的行數(shù)據(jù)了。如果不自動產(chǎn)生列,則需手動添加列,把列的數(shù)據(jù)源屬性名(DataPropertyName)設置為對應數(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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
UGUI實現(xiàn)隨意調(diào)整Text中的字體間距
這篇文章主要為大家詳細介紹了UGUI實現(xiàn)隨意調(diào)整字體間距的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03
C# 并發(fā)控制框架之單線程環(huán)境下實現(xiàn)每秒百萬級調(diào)度
本文介紹了一款專為工業(yè)自動化及機器視覺開發(fā)的C#并發(fā)流程控制框架,通過模仿Go語言并發(fā)模式設計,支持高頻調(diào)度及復雜任務處理,已在多個項目中驗證其穩(wěn)定性和可靠性2024-10-10
C#實現(xiàn)在網(wǎng)頁中根據(jù)url截圖并輸出到網(wǎng)頁的方法
這篇文章主要介紹了C#實現(xiàn)在網(wǎng)頁中根據(jù)url截圖并輸出到網(wǎng)頁的方法,涉及C#網(wǎng)頁瀏覽器及圖片操作的相關技巧,需要的朋友可以參考下2016-01-01
C# Winform截圖指定控件范圍內(nèi)的圖像的流程步驟
工作所需,需要截圖軟件跑出來的界面上的圖表,但是窗口本身是可以縮放的,圖表也是做的可以跟著窗體大小一起縮放,所以就寫了一個函數(shù),用于截圖圖表容器內(nèi)的圖像,文中有函數(shù)源碼供大家參考,需要的朋友可以參考下2024-10-10
基于C#實現(xiàn)Windows服務狀態(tài)啟動和停止服務的方法
這篇文章主要介紹了基于C#實現(xiàn)Windows服務狀態(tài)啟動和停止服務的方法,詳細講述了實現(xiàn)這一功能的具體步驟,代碼簡潔易懂,需要的朋友可以參考下2014-09-09

