2014-09-24 123 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