无法在c#中的datagridview中选中复选框

时间:2013-05-17 19:29:41

标签: c# .net datagridview

我在c#windows应用程序中有以下代码,我试图插入对应于每行的复选框。当我单击所选行之外的按钮时应删除。但我无法使用此代码选中复选框。

dataGridView1.ReadOnly = false;
OleDbCommand SQLQuery = new OleDbCommand();
DataTable data = null;
dataGridView1.DataSource = null;
SQLQuery.Connection = null;
OleDbDataAdapter dataAdapter = null;
dataGridView1.Columns.Clear(); 

SQLQuery.CommandText = sqlQueryString;
SQLQuery.Connection = database;
data = new DataTable();
dataAdapter = new OleDbDataAdapter(SQLQuery);
dataAdapter.Fill(data);
dataGridView1.DataSource = data;
dataGridView1.AllowUserToAddRows = true; // remove the null line
dataGridView1.ReadOnly = true;
dataGridView1.Columns[0].Visible = true;
dataGridView1.Columns[1].Width = 340;
dataGridView1.Columns[2].Width = 55;          
// insert checkbox button into datagridview
deleteck = new DataGridViewCheckBoxColumn();
deleteck.HeaderText = "delete";     
deleteck.Width = 80;
deleteck.ReadOnly = false;         
dataGridView1.Columns.Insert(0, deleteck); 

有谁能帮我弄清楚这里有什么问题?

3 个答案:

答案 0 :(得分:1)

我找到了这个解决方案:

    dataGridView1.DataSource = data;
    For Each dc as DataColumn in dt.Columns
        dc.ReadOnly = true
    Next

    dataGridView1.AllowUserToAddRows = true; 
    // dataGridView1.ReadOnly = true;
    dataGridView1.Columns[0].Visible = true;
    dataGridView1.Columns[1].Width = 340;
    dataGridView1.Columns[2].Width = 55;          
    deleteck = new DataGridViewCheckBoxColumn();
    deleteck.HeaderText = "delete";     
    deleteck.Width = 80;
    deleteck.ReadOnly = false;         
    dataGridView1.Columns.Insert(0, deleteck);

这样,只有复选框是可编辑的,而其余的列集合在数据源级别被标记为只读。

答案 1 :(得分:0)

我的代码错了。

dataGridView1.ReadOnly = true;

当我刚评论上述内容时,它起作用了。

答案 2 :(得分:0)

您必须选中DataGridView任务列表中的启用编辑复选框。这是屏幕截图

enter image description here