【问题标题】:Change row color in datagridview更改datagridview中的行颜色
【发布时间】:2020-08-07 06:16:44
【问题描述】:

我知道它已经被写过了。 一切看起来都很好。但是当我向右移动以查看其余列时,DataDridView 中的行开始非常闪烁。我无法解决这个问题。

private void registersDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    DataGridViewRow rowDataGridView = null;

    string dataPropertyName;
    dataPropertyName = this.registersDataGridView.Columns[e.ColumnIndex].DataPropertyName;

    int isApprovedColumnIndex = this.registersDataGridView.Columns[isApprovedColumnName].Index;
    int isCancelledColumnIndex = this.registersDataGridView.Columns[isCancelledColumnName].Index;

    bool theColorHasBeenSet = false;

    if (e.RowIndex >= 0 && e.RowIndex != btregisterDgvRowIndex)
    {
        rowDataGridView = this.registersDataGridView.Rows[e.RowIndex];

        if (this.registersDataGridView.Columns[isCancelledColumnIndex].DataPropertyName == "IsCancelled")
        {
            if (rowDataGridView.Cells[isCancelledColumnIndex].Value != null && rowDataGridView.Cells[isCancelledColumnIndex].Value.ToString() == "Tak")
            {
                if (rowDataGridView.DefaultCellStyle.BackColor != Color.PaleVioletRed)
                {
                    rowDataGridView.DefaultCellStyle.BackColor = Color.PaleVioletRed;
                }
                theColorHasBeenSet = true;
            }
            else
            {
                if (rowDataGridView.DefaultCellStyle.BackColor != Color.Ivory)
                {
                    rowDataGridView.DefaultCellStyle.BackColor = Color.Ivory;
                }
            }
            btregisterDgvRowIndex = e.RowIndex;
        }

        if (this.registersDataGridView.Columns[isApprovedColumnIndex].DataPropertyName == "IsApproved")
        {
            if (!theColorHasBeenSet)
            {
                if (rowDataGridView.Cells[isApprovedColumnName].Value != null && rowDataGridView.Cells[isApprovedColumnName].Value.ToString() == "-")
                {
                    if (rowDataGridView.DefaultCellStyle.BackColor != Color.LightGray)
                    {
                        rowDataGridView.DefaultCellStyle.BackColor = Color.LightGray;
                    }
                    theColorHasBeenSet = true;
                }
                else if (rowDataGridView.Cells[isApprovedColumnName].Value != null && rowDataGridView.Cells[isApprovedColumnName].Value.ToString() == "Nie")
                {
                    if (rowDataGridView.DefaultCellStyle.BackColor != Color.PaleVioletRed)
                    {
                        rowDataGridView.DefaultCellStyle.BackColor = Color.PaleVioletRed;
                    }
                    theColorHasBeenSet = true;
                }
                else
                {
                    if (rowDataGridView.DefaultCellStyle.BackColor != Color.Ivory)
                    {
                        rowDataGridView.DefaultCellStyle.BackColor = Color.Ivory;
                    }
                }
                btregisterDgvRowIndex = e.RowIndex;
            }
        }
    }
}

【问题讨论】:

标签: c#


【解决方案1】:
        else
        {
            if (rowDataGridView.DefaultCellStyle.BackColor != Color.Ivory)
            {
                rowDataGridView.DefaultCellStyle.BackColor = Color.Ivory;
            }
        }

您没有在此处设置 theColorHasBeenSet,这可能会导致它在 Ivory 和列表​​中的下一个颜色之间变化。

【讨论】:

  • 太棒了。如果有帮助,您可以投票/标记答案吗?谢谢。
【解决方案2】:

您的代码对我来说似乎很冗长,请尝试以下操作

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.RowIndex < 0)
        return;

    var rowDataGridView = this.registersDataGridView.Rows[e.RowIndex];

    Color GetBackgroundColor()
    {
        var isApprovedColumnIndex = this.registersDataGridView.Columns[isApprovedColumnName].Index;
        var isCancelledColumnIndex = this.registersDataGridView.Columns[isCancelledColumnName].Index;

        if (this.registersDataGridView.Columns[isCancelledColumnIndex].DataPropertyName == "IsCancelled")
        {
            var strValue = Convert.ToString(rowDataGridView.Cells[isCancelledColumnIndex].Value);
            if (strValue == "Tak")
                return Color.PaleVioletRed;
        }

        if (this.registersDataGridView.Columns[isApprovedColumnIndex].DataPropertyName == "IsApproved")
        {
            var strValue = Convert.ToString(rowDataGridView.Cells[isApprovedColumnIndex].Value);
            if (strValue == "-")
                return Color.LightGray;
            if (strValue == "Nie")
                return Color.PaleVioletRed;
        }

        return Color.Ivory;
    }

    rowDataGridView.DefaultCellStyle.BackColor = GetBackgroundColor();
}

【讨论】:

  • 它还在闪烁。
  • @Wieslaw - 更新了我的代码,如果你愿意,可以试试
猜你喜欢
  • 1970-01-01
  • 2011-01-12
  • 2015-09-07
  • 1970-01-01
  • 2013-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多