如何在C#中动态生成在DataGridView中检查的CheckBox?

时间:2014-10-09 01:02:34

标签: c#-4.0 checkbox datagridview

有一个DataGridView有一个CheckBox列。我从表中生成DataGridView的其他列。它工作正常,但现在我尝试使用此代码检查一些复选框,但它无法正常工作。代码看起来像

string query = "SELECT ID, Group_Name+' '+Phone_No as Info FROM Group_Info";
GenerateGridView(dataGridView1, query);

DataTable dt = GetTableData("SELECT Group_ID FROM tblGenerate");

foreach(DataRow rw in dt.Rows )
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)(row.Cells[0].Value );

        if (Convert.ToInt32(row.Cells[1].Value) == Convert.ToInt32(rw["Group_ID"]))
        {
            chk.Value = chk.TrueValue;
        }
    }
}

我们怎么做?

1 个答案:

答案 0 :(得分:2)

愿你试试这个:

            DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
            dataGridView1.Columns.Add(chk);
            chk.HeaderText = "Check Data";
            chk.Name = "chk";
            dataGridView1.Rows[2].Cells[3].Value = true;
相关问题