繁体   English   中英

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

[英]Inactive checkbox in datagridview VB.NET

我像这样向datagridview添加了复选框

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

当我运行它时,它可以正确显示,但是现在我想动态禁用dataGridView上的某些行,而不是每一行,只是某些行取决于该行中的其他值,我的意思是,当column2具有值“ Open”时,我尝试这样做

 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我该怎么办非常感谢

尝试这个 :

 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;

    }

}

为什么不像刚才提到的那样只禁用单元格(只读),还要将其设置为BackColor

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

您可以通过禁用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

请检查以下答案: 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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM