国产探花免费观看_亚洲丰满少妇自慰呻吟_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。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武鸣县| 黄浦区| 固安县| 牡丹江市| 龙井市| 调兵山市| 美姑县| 额济纳旗| 抚宁县| 龙州县| 如皋市| 抚宁县| 菏泽市| 永昌县| 遂昌县| 榆林市| 金秀| 天祝| 谢通门县| 凤庆县| 文安县| 宁远县| 湖南省| 富裕县| 大英县| 肇东市| 格尔木市| 牡丹江市| 澎湖县| 靖州| 辽宁省| 南漳县| 渑池县| 元氏县| 平阴县| 阿拉善左旗| 株洲市| 西盟| 内黄县| 莫力| 芜湖市|