datagridview實(shí)現(xiàn)手動(dòng)添加行數(shù)據(jù)
datagridview手動(dòng)添加行數(shù)據(jù)
我在做軟件模型界面時(shí),通過(guò)功能按鈕觸發(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);另外,訪問(wèn)行單元格的方式可以這樣
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)文章
UGUI實(shí)現(xiàn)隨意調(diào)整Text中的字體間距
這篇文章主要為大家詳細(xì)介紹了UGUI實(shí)現(xiàn)隨意調(diào)整字體間距的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
C# 并發(fā)控制框架之單線程環(huán)境下實(shí)現(xiàn)每秒百萬(wàn)級(jí)調(diào)度
本文介紹了一款專為工業(yè)自動(dòng)化及機(jī)器視覺(jué)開(kāi)發(fā)的C#并發(fā)流程控制框架,通過(guò)模仿Go語(yǔ)言并發(fā)模式設(shè)計(jì),支持高頻調(diào)度及復(fù)雜任務(wù)處理,已在多個(gè)項(xiàng)目中驗(yàn)證其穩(wěn)定性和可靠性2024-10-10
C#實(shí)現(xiàn)在網(wǎng)頁(yè)中根據(jù)url截圖并輸出到網(wǎng)頁(yè)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)在網(wǎng)頁(yè)中根據(jù)url截圖并輸出到網(wǎng)頁(yè)的方法,涉及C#網(wǎng)頁(yè)瀏覽器及圖片操作的相關(guān)技巧,需要的朋友可以參考下2016-01-01
C# Winform截圖指定控件范圍內(nèi)的圖像的流程步驟
工作所需,需要截圖軟件跑出來(lái)的界面上的圖表,但是窗口本身是可以縮放的,圖表也是做的可以跟著窗體大小一起縮放,所以就寫了一個(gè)函數(shù),用于截圖圖表容器內(nèi)的圖像,文中有函數(shù)源碼供大家參考,需要的朋友可以參考下2024-10-10
C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
這篇文章主要介紹了C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法,涉及C#文件傳輸?shù)募记?具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
基于C#實(shí)現(xiàn)Windows服務(wù)狀態(tài)啟動(dòng)和停止服務(wù)的方法
這篇文章主要介紹了基于C#實(shí)現(xiàn)Windows服務(wù)狀態(tài)啟動(dòng)和停止服務(wù)的方法,詳細(xì)講述了實(shí)現(xiàn)這一功能的具體步驟,代碼簡(jiǎn)潔易懂,需要的朋友可以參考下2014-09-09

