简体   繁体   中英

Change row color in datagridview

I know it's already been written about. Everything looks very good. But when I move to the right to see the rest of the columns, the rows in DataDridView start blinking very much. I can't solve this.

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;
            }
        }
    }
}
        else
        {
            if (rowDataGridView.DefaultCellStyle.BackColor != Color.Ivory)
            {
                rowDataGridView.DefaultCellStyle.BackColor = Color.Ivory;
            }
        }

You're not setting the theColorHasBeenSet here which might cause it to change between Ivory and the next color on your list.

Your code seems to verbose to me, try the following

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM