日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Winform中给DataGridView添加多选框列并获取选中行的内容

發布時間:2025/3/19 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Winform中给DataGridView添加多选框列并获取选中行的内容 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

場景

使用NPOI導入Excel并賦值給DataTable,然后顯示在DataGrdView上,并且添加多選框,然后獲取選中行的內容。

Winform中使用NPOI實現Excel導入并賦值給DataTable:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100064821

實現

給DataGridView添加多選框,而不是給DataTable添加多選框。

DataGridViewColumn checkCol = new DataGridViewCheckBoxColumn(); this.dataGridView1.Columns.Add(checkCol);

獲取選中行的內容

private void button4_Click(object sender, EventArgs e){int strCount = 0;//首先進行第一次循環 獲取選中的行數for (int i = 0; i <dataGridView1.Rows.Count; i++){//如果被選中if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true){strCount++;}}//新建 選中長度的數組存放每一行string[] str = new string[strCount];for (int i = 0; i < dataGridView1.Rows.Count; i++){if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true){//獲取當前行的第一個單元格 + @ +第二個單元格的內容str[i] = dataGridView1.Rows[i].Cells[1].Value.ToString() + "@" + dataGridView1.Rows[i].Cells[2].Value.ToString();}}//輸出選中所有行的內容for (int i = 0; i < str.Length; i++){MessageBox.Show(string.Format("獲取的第{0}條為:",i+1) +str[i]);}}

效果

?

總結

以上是生活随笔為你收集整理的Winform中给DataGridView添加多选框列并获取选中行的内容的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。