2014-01-08 99 views
1

我有一個綁定到DataSet的DataGridView。我有一個CheckBox列。當我希望行在用戶檢查特定行時立即改變顏色。我可以用我的代碼改變顏色,但由於某種原因,我不知道..只有當我離開單元格時纔會改變顏色。選中CheckBox時更改DataGridView Row的顏色。

Private Sub DataGridView1_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged 
     If DataGridView1.Columns(e.ColumnIndex).Name = "ColCheck" Then 
      If DataGridView1.Rows(e.RowIndex).Cells("ColCheck").Value = True Then 

       DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.LightGreen 
       ''' blah blah blah... 
+0

有關如何添加' DataGridView1.Refresh()'? –

+0

仍是同樣的故事。 – Arbaaz

+0

使用CellEndEdit ... – Codexer

回答

0

寫這個代碼上面

dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit); 
0

嘗試......

Private Sub dataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) 
If DataGridView1.Columns(e.ColumnIndex).Name = "ColCheck" Then 
     If DataGridView1.Rows(e.RowIndex).Cells("ColCheck").Value = True Then 

    Dim isChecked As Boolean = DirectCast(dataGridView1(e.ColumnIndex, e.RowIndex).FormattedValue, [Boolean]) 
    If isChecked Then 
       DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.LightGreen 
    End If 
End If 
    End If 
End Sub 
+0

一起工作請確保添加...將CellValueChanged處理爲子結束。由於某種原因,它不適合我。 – Codexer

+0

This code not working ... 但是,我找到了解決方案..謝謝你的回答。 – Arbaaz

+0

比我不知道只是跑了這個,爲我工作?祝你好運 – Codexer

0

事實證明,我必須使用CurrentCellDirtyStateChanged

Private Sub DataGridView1_CurrentCellDirtyStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged 
     If DataGridView1.IsCurrentCellDirty Then 
      DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit) 
     End If 
    End Sub 
+0

當然,骯髒的一個大聲笑,有更多的方式比一個;) – Codexer

+0

正如你所看到的,我還沒有標記我的答案是答案,如果有人可以給我另一個代碼的作品..我會更多比很高興接受這一點。另外我會得到+2:D – Arbaaz