Изменить цвет строки в datagridview - PullRequest
0 голосов
/ 07 августа 2020

Я знаю, что об этом уже писали. Все выглядит очень хорошо. Но когда я двигаюсь вправо, чтобы увидеть остальные столбцы, строки в 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;
            }
        }
    }
}

Ответы [ 2 ]

0 голосов
/ 07 августа 2020

Ваш код кажется мне многословным, попробуйте следующее

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();
}
0 голосов
/ 07 августа 2020
        else
        {
            if (rowDataGridView.DefaultCellStyle.BackColor != Color.Ivory)
            {
                rowDataGridView.DefaultCellStyle.BackColor = Color.Ivory;
            }
        }

Вы не устанавливаете здесь theColorHasBeenSet, что может привести к его изменению между цветом слоновой кости и следующим цветом в вашем списке.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...