2014-09-24 203 views
0

我添加复选框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我应该怎么做比KS这么多

回答

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; 

    } 

} 
+0

FlatStyle.Flat可以工作。 – loveisbug 2015-04-03 11:19:08

0

你为什么不只是禁用该小区作为你提到的(只读),也设置它的BackColor

dgvDetail.Item(1, i).Style.BackColor = Color.LightGray 
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