C# 如何使ultragrid行基于单元格的值具有不同的背景色?
我的winform应用程序中有一个infragistics UltraGridView,希望根据特定的单元格值为不同的行指定不同的颜色。例如,如果单元格[状态]等于2,则该行应为蓝色,以此类推 我可以在初始化行执行此操作,但这还不够。因为状态可以在代码中更改,颜色也应该相应地更改。实现这一目标最有效的方法是什么?欢迎使用示例代码 网格绑定到对象的C# 如何使ultragrid行基于单元格的值具有不同的背景色?,c#,.net,winforms,gridview,infragistics,C#,.net,Winforms,Gridview,Infragistics,我的winform应用程序中有一个infragistics UltraGridView,希望根据特定的单元格值为不同的行指定不同的颜色。例如,如果单元格[状态]等于2,则该行应为蓝色,以此类推 我可以在初始化行执行此操作,但这还不够。因为状态可以在代码中更改,颜色也应该相应地更改。实现这一目标最有效的方法是什么?欢迎使用示例代码 网格绑定到对象的bindingSource,而bindingSource.dataSource是对象的bindingslist private void dgvProd
bindingSource
,而bindingSource.dataSource
是对象的bindingslist
private void dgvProduction_InitializeRow(object sender, InitializeRowEventArgs e)
{
switch (e.Row.Cells["StatusId"].Value.ToString())
{
case "0":
e.Row.Appearance.BackColor = Color.Gray;
break;
case "1":
e.Row.Appearance.BackColor = Color.White;
break;
case "2":
e.Row.Appearance.BackColor = Color.LightSkyBlue;
break;
}
}
我们通过创建两个单独的方法来实现这一点,一个名为ApplyRowHighlights的方法迭代每行并调用第二个名为ApplySingleRowHighlight的方法。然后,无论何时加载网格(由网格的GridLoaded事件触发),我们都调用ApplyRowHighlights方法。在对给定行进行修改之后,我们还为单个行调用ApplySingleRowHighlight方法 以下是上述方法的一些简化示例方法:
Private Sub pg_GridLoaded() Handles pg.GridLoaded
ApplyRowHighlights()
End Sub
Private Sub ApplyRowHighlights()
Dim intCount As Integer = 0
For Each oRow In pg.ugData.Rows.GetRowEnumerator(GridRowType.DataRow, Nothing, Nothing)
If oRow.VisibleIndex > -1 Then
ApplySingleRowHighlight(oRow, intCount)
End If
Next
' intCount used in record count method
End Sub
Private Sub ApplySingleRowHighlight(oRow As UltraGridRow, ByRef intCount As Integer)
If oRow Is Nothing Then Exit Sub
oRow.Appearance.BackColor = MDI.ApSettings.NeutralHighlight
If oRow.Cells.Exists("IsStandard") Then
If Not IsDBNull(oRow.Cells("IsStandard").Value) Then
If CBool(oRow.Cells("IsStandard").Value) Then
oRow.Appearance.BackColor = MDI.ApSettings.CalibrationStandardHighlight
End If
End If
End If
ApplyPastDueRowHighlighting(oRow, intCount, "CalibrationDue", "FrequencyGuid", "FrequencyUnits", MDI.ApSettings.PastDueHighlight, MDI.ApSettings.SoonDueHighlight)
End Sub
下面是一个WinForm应用程序的示例,它根据UltraGrid中的值应用不同的行格式
我们通过创建两个单独的方法来实现这一点,一个称为ApplyRowLights的方法迭代每行并调用第二个名为ApplySingleRowHighlight的方法。然后,无论何时加载网格(由网格的GridLoaded事件触发),我们都调用ApplyRowHighlights方法。在对给定行进行修改之后,我们还为单个行调用ApplySingleRowHighlight方法 以下是上述方法的一些简化示例方法:
Private Sub pg_GridLoaded() Handles pg.GridLoaded
ApplyRowHighlights()
End Sub
Private Sub ApplyRowHighlights()
Dim intCount As Integer = 0
For Each oRow In pg.ugData.Rows.GetRowEnumerator(GridRowType.DataRow, Nothing, Nothing)
If oRow.VisibleIndex > -1 Then
ApplySingleRowHighlight(oRow, intCount)
End If
Next
' intCount used in record count method
End Sub
Private Sub ApplySingleRowHighlight(oRow As UltraGridRow, ByRef intCount As Integer)
If oRow Is Nothing Then Exit Sub
oRow.Appearance.BackColor = MDI.ApSettings.NeutralHighlight
If oRow.Cells.Exists("IsStandard") Then
If Not IsDBNull(oRow.Cells("IsStandard").Value) Then
If CBool(oRow.Cells("IsStandard").Value) Then
oRow.Appearance.BackColor = MDI.ApSettings.CalibrationStandardHighlight
End If
End If
End If
ApplyPastDueRowHighlighting(oRow, intCount, "CalibrationDue", "FrequencyGuid", "FrequencyUnits", MDI.ApSettings.PastDueHighlight, MDI.ApSettings.SoonDueHighlight)
End Sub
下面是一个WinForm应用程序的示例,它根据UltraGrid中的值应用不同的行格式
用户或代码隐藏者更改单元格值时,总是会触发InitializeRow。所以你的代码应该可以工作。检查是否正在执行阻止行更新的其他操作。由用户或代码更改单元格值将始终触发InitializeRow。所以你的代码应该可以工作。检查您是否正在执行阻止行更新的其他操作。