如何检查选择了哪个datagridview复选框

时间:2012-10-08 11:54:34

标签: c# datagridview

我搜索得很厉害,无法找到如何检查选择了哪个datagridview复选框

我正在使用C#代码,我想知道如何能够检查选择了哪些复选框并保留行索引以便我可以查看数据网格视图信息

我已经添加了复选框的

这是我目前的代码“

dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].Name = "Product ID";
            dataGridView1.Columns[1].Name = "Product Name";
            dataGridView1.Columns[2].Name = "Product Price";

            string[] row = new string[] { "1", "Product 1", "1000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "2", "Product 2", "2000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "3", "Product 3", "3000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "4", "Product 4", "4000" };
            dataGridView1.Rows.Add(row);

            DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
            dataGridView1.Columns.Add(chk);
            chk.HeaderText = "Check Data";
            chk.Name = "chk";

2 个答案:

答案 0 :(得分:3)

尝试使用此代码:

for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {


            GridViewRow row = GridView1.Rows[i];
            CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox2");
            if (Ckbox.Checked)
            {
               //........

            }
        }

答案 1 :(得分:1)

foreach(DataGridViewRow row in datagridview.Rows)
{
    foreach(DataGridViewCell cell in row.Cells)
    {
        if(cell is DataGridViewCheckBoxCell)
        {
            isChecked = (datagridview.GetChildByPoint(new Point(cell.ColumnIndex, cell.RowIndex)) as CheckBox).Checked;
        }
    }
}

这可能会解决您的问题。如果您已设置列的TrueValue和FalseValue,则可以更轻松地根据这些值检查当前值并推断其检查状态。