C# winform datagridview 複選

來源:互聯網
上載者:User

在Web程式中資料來源繫結控制項GridView中有模板列,而要擷取其中的複選框是否選中以及複選框所在的行是很簡單的,經典代碼如下:
     for(int i=0;i<gvList.Rows.Count;i++)
     {
         if(((CheckBox)gvList.Rows[i].Cells[0].FindControl("chkID")).Checked)
         { }
     }
         但是在WinForm中的DataGridView控制項卻不能這樣來做,DataGridView控制項根本沒有自由模板列,有的是6個內建模板列,而複選框正是其中之一。要獲得複選框是否選中以及選中行對應的其他值是不能按照Web程式中的這種方式來實現的,而是需要通過DataGridView控制項中Cells儲存格的Value屬性來判斷的。

        (WinForm中DataGridView控制項通過複選框實現多條記錄的刪除)代碼如下:
         private void btnDelete_Click(object sender, EventArgs e)
         {
             string strNames = "您選擇的是:";
             for(int i=0;i<dgvList.Rows.Count;i++)
             {
                 if (dgvList.Rows[i].Cells[0].Value != null) //判斷該行的複選框是否存在
                 {
                     if (dgvList.Rows[i].Cells[0].Value.ToString() == "True") //判斷該複選框是否被選中
                     {
                         strNames += dgvList.Rows[i].Cells[2].Value.ToString() + "   ";
                     }
                 }
             }
             MessageBox.Show(strNames, "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         運行結果:

 轉載自網路,來源不明!!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.