2009-09-16 2 views
1

VB .Net 3.5에서는 DataGridViewCell (바인딩되지 않음)의 색을 다른 색으로 변경할 수 있습니까? 포커스가 사라지거나 셀을 떠나기 전에 셀이 눈에 띄게 바뀌지 않습니까?? 데이터가있는 쿼리를 실행하는 타이머가 있고 사용자가 셀을 떠난 후에 대신 색상을 즉시 변경하고 싶습니다.DataGridViewCell 포커스를 잃지 않고 배경색 변경

DataGridView.Refresh 및 Me.Refresh를 시도했지만 결과를 얻지 못했습니다.

내가 뭘 잘못하고 있니? 그런 다음 당신이 행 내 경우에는, 예를 들어, 개의 다른 색으로 그려 질 때 (사용자가 CheckBoxCell를 클릭 할 때) 자신을 설명해야

''' <summary> 
''' Sets or clears the passed cells background to Red 
''' </summary> 
''' <param name="ColumnNumber">Datagridview column index of the cell to be changed</param> 
''' <param name="RowNumber">Datagridview row index of the cell to be changed</param> 
''' <param name="Error">Indicate whether the cell should be red, <c>True</c>, or empty, <c>False</c></param> 
Private Sub Error_Cell(ByVal ColumnNumber As Integer, ByVal RowNumber As Integer, ByVal [Error] As Boolean) 
    If [Error] Then 
     Me.dgvMain.Rows(RowNumber).Cells(ColumnNumber).Style.BackColor = Color.Red 
    Else 
     Me.dgvMain.Rows(RowNumber).Cells(ColumnNumber).Style.BackColor = Color.Empty 
    End If 
    Me.dgvMain.Refresh() 
    Me.Refresh() 
End Sub 

답변

0

현재 셀 배경색 만 변경해야하고 DataGridViewCell의 EditControl을 사용하기로 결정했습니다.

캡쳐하려면 DataGridView 이벤트 "EditControlShowing"에서 EditControl (Type DataGridViewTextBoxEditingContorl)을 가져 와서 EditControl 셀을 로컬 양식 변수와 연결 한 다음 TextChagned 이벤트에 처리기를 추가해야합니다. TextChanged는 편집이 완료 될 때까지 발생하지 않습니다. (첫 번째 절차).

이렇게하면 EditControls BackColor를 변경하여 셀의 색을 변경할 수 있습니다.이 색은 즉시 변경됩니다. (두 번째 프로 시저)

내 개인 변수를 다시 사용하려면 DataGridViews CellEndEdit 이벤트에서 EditControl을 해제해야합니다. (세 번째 절차)

사용 변경으로 인해 행을 변경하려고 시도한 것이 테스트되지 않았지만 제대로 작동하는 것 같습니다. 이 문제가 비슷한 사람들에게 도움이되기를 바랍니다. 더 효율적인 방법이 있다면 알려주십시오!

Private EditingControl As DataGridViewTextBoxEditingControl 

Private Sub dgvMain_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvMain.EditingControlShowing 
    If TypeOf (e.Control) Is DataGridViewTextBoxEditingControl Then 
     EditingControl = DirectCast(e.Control, DataGridViewTextBoxEditingControl) 
     If DirectCast(e.Control, DataGridViewTextBoxEditingControl).EditingControlDataGridView.CurrentCell.OwningColumn Is PartNumber Then 
      AddHandler EditingControl.TextChanged, AddressOf EditingControl_TextChanged 
     End If 
    End If 
End Sub 

Private Sub Error_Cell(ByVal [Error] As Boolean) 
    If [Error] Then 
     If EditingControl IsNot Nothing Then EditingControl.BackColor = Color.Red 
    Else 
     If EditingControl IsNot Nothing Then EditingControl.BackColor = Color.Empty 
    End If 
    Me.dgvMain.Refresh() 
End Sub 



Private Sub dgvMain_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvMain.CellEndEdit 
    If EditingControl IsNot Nothing Then 
     RemoveHandler EditingControl.TextChanged, AddressOf EditingControl_TextChanged 
    End If 

    EditingControl = Nothing 

End Sub 

참고 : 내가 만일/그럼 가능하면 무죄, 그렇지 않으면, 그들은 인라인 것 보호하기 위해 제거 된 내부에 가지고있는 많은 단계.

0

먼저 (아래 내가 배경을 변경하는 데 사용하는 코드입니다) CellLeave 또는 CellContentClick과 같은 diferents 이벤트를 사용해보십시오 (예 : 마지막으로 코드를 사용하십시오!

void updateCellStyle_DataGridViewCellEventArgs(object sender, DataGridViewCellEventArgs e){ 
int index = e.RowIndex; 
if (index == -1) 
return; 
else { 
DataGridView dgv = sender as DataGridView; 
int vCHK = e.ColumnIndex; //this is the checkbox column 
if (vCHK != 0) 
return; 
else { 
DataGridViewCheckBoxCell temp = (DataGridViewCheckBoxCell)dgv.Rows[index].Cells[0]; 
if ((bool)temp.EditedFormattedValue == true) { 
DataGridViewTextBoxCell xrow = (DataGridViewTextBoxCell)dgv.Rows[index].Cells[3]; 
xrow.OwningRow.DefaultCellStyle.BackColor = Color.Wheat; 
/* 
other operations 
*/ 
} 
else { 
temp.OwningRow.DefaultCellStyle.BackColor = Color.White; 
/* 
other operations 
*/ 
} 
+0

사용자가 잘못된 부품 번호를 입력하면 행을 다른 색으로 칠합니다. 그들이 다른 곳으로 이동하기 전에 셀은 색상을 변경하여 올바른 부분을 사용하지 않는다고 알립니다. 나는 이것에 대해 며칠 동안 놀고 있었기 때문에 나는 너희들에게 물어볼 것이라고 생각했다. – Stevoni

관련 문제