国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C# DataGridView添加新行的2個方法

2020-01-24 03:34:38
字體:
來源:轉載
供稿:網友

可以靜態綁定數據源,這樣就自動為DataGridView控件添加 相應的行。假如需要動態為DataGridView控件添加新行,方法有很多種,下面簡單介紹如何為DataGridView控件動態添加新行的兩種方 法:


方法一:

復制代碼 代碼如下:

int index=this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index].Cells[0].Value = "1";
this.dataGridView1.Rows[index].Cells[1].Value = "2";
this.dataGridView1.Rows[index].Cells[2].Value = "監聽";

利用dataGridView1.Rows.Add()事件為DataGridView控件增加新的行,該函數返回添加新行的索引號,即新行的行號,然后可以通過該索引號操作該行的各個單元格,如dataGridView1.Rows[index].Cells[0].Value = "1"。這是很常用也是很簡單的方法。


方法二:

復制代碼 代碼如下:

DataGridViewRow row = new DataGridViewRow();
DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
textboxcell.Value = "aaa";
row.Cells.Add(textboxcell);
DataGridViewComboBoxCell comboxcell = new DataGridViewComboBoxCell();
row.Cells.Add(comboxcell);
dataGridView1.Rows.Add(row);

方法二比方法一要復雜一些,但是在一些特殊場合非常實用,例如,要在新行中的某些單元格添加下拉框、按鈕之類的控件時,該方法很有幫助。
DataGridViewRow row = new DataGridViewRow(); 是創建DataGridView的行對象,DataGridViewTextBoxCell是單元格的內容是個 TextBox,DataGridViewComboBoxCell是單元格的內容是下拉列表框,同理可知,DataGridViewButtonCell是單元格的內容是個按鈕,等等。textboxcell是新創建的單元格的對象,可以為該對象添加其屬性。然后通過row.Cells.Add(textboxcell)為row對象添加textboxcell單元格。要添加其他的單元格,用同樣的方法即可。
最后通過dataGridView1.Rows.Add(row)為dataGridView1控件添加新的行row。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 互助| 金川县| 泊头市| 威宁| 郯城县| 桦南县| 吉水县| 岳普湖县| 镇雄县| 珠海市| 公主岭市| 且末县| 贵溪市| 马公市| 洪泽县| 麻江县| 塔河县| 松江区| 姜堰市| 囊谦县| 南城县| 郑州市| 东宁县| 大理市| 广灵县| 柞水县| 龙山县| 酉阳| 雅安市| 塔河县| 山东省| 漳州市| 新巴尔虎右旗| 南城县| 灯塔市| 龙胜| 济南市| 南投市| 翁源县| 罗平县| 海安县|