c#中datagridview裡checkbox的使用方法

iDotNetSpace發表於2009-07-30

1、屬性設定checkboxcolumn

name:cb_check     falsevalue:false    truevalue:true
datagridview中的readonly設定為false.


2、  //單項選擇設定
private void dgv_zy_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int count = Convert.ToInt16(dgv_zy.Rows.Count.ToString());
                    for (int i = 0; i < count; i++)

{
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dgv_zy.Rows[i].Cells["cb_check"];
                Boolean flag = Convert.ToBoolean(checkCell.Value);
                if (flag == true)     //查詢被選擇的資料行
                {
                    checkCell.Value = false;
                }
                    continue;
             }
        }

 

3、獲取選擇的資料
            int count = Convert.ToInt32(dgv_zy.Rows.Count.ToString());
            for (int i = 0; i < count; i++)
            {
               //如果DataGridView是可編輯的,將資料提交,否則處於編輯狀態的行無法取到
                      dgv_zy.EndEdit();
                DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dgv_zy.Rows[i].Cells["cb_check"];
                Boolean flag = Convert.ToBoolean(checkCell.Value);
               if (flag == true)     //查詢被選擇的資料行
                {
                    //從 DATAGRIDVIEW 中獲取資料項
               string z_zcode = dgv_zy.Rows[i].Cells[0].Value.ToString().Trim();
              //........................................                }
         }

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-610896/,如需轉載,請註明出處,否則將追究法律責任。

相關文章