gpt4 book ai didi

c# - 根据条件更改 datagridview 单元格颜色

转载 作者:IT王子 更新时间:2023-10-29 04:15:44 25 4
gpt4 key购买 nike

我已将数据从数据库加载到 datagridview,并且有两列目标值和体积,其中体积 > 目标值,体积单元格应为绿色,体积 < 目标值,则体积应为红色。我试过了,但我做不到。

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
if (dataGridView1.Rows.Count > 0 && dataGridView1.Columns.Count > 0)
{
foreach (DataGridViewRow r in dataGridView1.Rows)
{
if (Volume > target value)
{
cell.Style.BackColor = Color.AliceBlue;
}

最佳答案

我可能会建议不要在每次调用 CellFormating 时遍历每一行,因为每次需要刷新单个行时都会调用它。

Private Sub dgv_DisplayData_Vertical_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgv_DisplayData_Vertical.CellFormatting
Try

If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells("LevelID").Value.ToString() = "6" Then

e.CellStyle.BackColor = Color.DimGray
End If
If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells("LevelID").Value.ToString() = "5" Then
e.CellStyle.BackColor = Color.DarkSlateGray
End If
If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells("LevelID").Value.ToString() = "4" Then
e.CellStyle.BackColor = Color.SlateGray
End If
If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells("LevelID").Value.ToString() = "3" Then
e.CellStyle.BackColor = Color.LightGray
End If
If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells("LevelID").Value.ToString() = "0" Then
e.CellStyle.BackColor = Color.White
End If

Catch ex As Exception

End Try

End Sub

关于c# - 根据条件更改 datagridview 单元格颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19380279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com