datagridview VB.NET中的非活动复选框

时间:2014-09-24 04:57:11

标签: c# sql-server vb.net

我添加了datagridview这样的复选框

 Dim CbxColumn As New DataGridViewCheckBoxColumn
                With CbxColumn
                    .HeaderText = ""
                    .Name = "Return"
                    .Width = 50
                End With
                dgvDetail.Columns.Insert(0, CbxColumn)

当我运行它显示正确,但现在我想动态地禁用dataGridView上的一些行而不是每一行只是某些行依赖于行中的其他值我的意思是当column2具有值"打开"我试着这样做

 For i = 0 To dgvDetail.Rows.Count - 1
                    If dgvDetail.Rows(i).Cells(1).Value = "Open" Then
     //I want to do what i expect here//
                        dgvDetail.Rows(i).Cells(1).ReadOnly = True
                    End If
                Next

但它不能编辑价值,但我更喜欢将其禁用为灰色或非活动控件,就像我们设置时buttoncontrol.enabled=false我该怎么办非常感谢

4 个答案:

答案 0 :(得分:1)

试试这个:

 private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    if (e.RowIndex == 1)
    {
        DataGridViewCell cell=dataGridView1.Rows[e.RowIndex].Cells[0];
        DataGridViewCheckBoxCell chkCell = cell as DataGridViewCheckBoxCell;
        chkCell.Value = false;
        chkCell.FlatStyle = FlatStyle.Flat;
        chkCell.Style.ForeColor = Color.DarkGray;
        cell.ReadOnly = true;

    }

}

答案 1 :(得分:0)

为什么不像你提到的那样(只读)禁用单元格,而且还设置它为BackColor

  dgvDetail.Item(1, i).Style.BackColor = Color.LightGray

答案 2 :(得分:0)

您可以通过禁用DataGridViewCell

来实现此目的
private void enableCell(DataGridViewCell row, bool enabled) {
    //toggle read-only state
    row.ReadOnly = !enabled;
    if (enabled)
    {
        //restore cell style to the default value
        row.Style.BackColor = row.OwningColumn.DefaultCellStyle.BackColor;
        row.Style.ForeColor = row.OwningColumn.DefaultCellStyle.ForeColor;
    }
    else { 
        //gray out the cell
        row.Style.BackColor = Color.LightGray;
        row.Style.ForeColor = Color.DarkGray;
    }
}

或者您可以通过遍历每个单元格来扩展上述代码以禁用整个DataGridViewRow

答案 3 :(得分:0)

请检查此答案:https://stackoverflow.com/a/1626948/1361234

我找到了一个项目:http://www.codeproject.com/Articles/31829/Disabled-Checkbox-Column-in-the-DataGridView

但是,我在@Thirisangu的回答中使用了这个方法,它可以工作。谢谢你,Thirisangu。

    chkCell.FlatStyle = FlatStyle.Flat;
    chkCell.Style.ForeColor = Color.DarkGray;
    cell.ReadOnly = true;