简体   繁体   English

C# Datagridview 仅根据条件更改行颜色前 1000 条记录

[英]C# Datagridview change row color based on condition only first 1000 records

i have a little problem with my datagridview.我的datagridview 有一点问题。 I am filling the Datagridview with my records from the db.我正在用数据库中的记录填充 Datagridview。

But i want to change the back- and forecolor in a specific condition.但我想在特定条件下更改背景色和前景色。 It works fine but only with the first 1000 records and the problem is i have more than 10.000 records.它工作正常,但只有前 1000 条记录,问题是我有超过 10.000 条记录。 Did i have a wrong event?我有错误的事件吗?

I'd be grateful for a better solution.如果有更好的解决方案,我将不胜感激。

Best regards此致

my code:我的代码:

private void dataView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    foreach(DataGridViewRow row in dataView.Rows)
    {
        int value = Convert.ToInt32(row.Cells[2].Value);

        if(value == 1)
        {
            row.DefaultCellStyle.BackColor = Color.Orange;
        }
        else
        {
            row.DefaultCellStyle.BackColor = Color.White;
        }
    }
}

The CellFormatting event is not the best option in your case. CellFormatting事件不是您的最佳选择。 Your for loop is causing you to iterate over your data multiple times.您的 for 循环导致您多次迭代数据。

You can use the RowPrePaint , RowPostPaint or the DataBindingComplete events for your scenario.您可以为您的方案使用RowPrePaintRowPostPaintDataBindingComplete事件。

Using the RowPrePaint will allow you to color the row and leave open the option to apply additional cell level styling for individual cells.使用RowPrePaint将允许您为行着色并保持打开选项以为单个单元格应用额外的单元格级别样式。

 private void dataView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
 {
     DataGridViewRow row = dataView.Rows[e.RowIndex];
     if (Convert.ToInt32(row.Cells[2].Value) == 1)
     {
         row.DefaultCellStyle.BackColor = Color.Orange;
     }
     else
     {
         row.DefaultCellStyle.BackColor = Color.White;
     }
 }

If you choose to keep your loop then you can use the DataBindingComplete event.如果您选择保留循环,则可以使用DataBindingComplete事件。

private void dataView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    foreach (DataGridViewRow row in dataView.Rows)
    {
        int value = Convert.ToInt32(row.Cells[2].Value);
        if (value == 1)
        {
            row.DefaultCellStyle.BackColor = Color.Orange;
        }
        else
        {
            row.DefaultCellStyle.BackColor = Color.White;
        }
    }
}

@quaabaam @quaabaam

thank you the first solution worked fine for me.谢谢你的第一个解决方案对我来说很好。 Can i change 2 Cells with this event?我可以通过此活动更改 2 个单元格吗? like this:像这样:

private void DataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
            //Secili olanlarin rengini degisdiriyo
            if (Convert.ToInt32(row.Cells["Secili"].Value) == 1)
            {
                this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightSalmon;
                this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Black;
            }
            else
            {
                //Aktiv olmiyanlari kirmizi yapiyo
                if (Convert.ToInt32(row.Cells["IsActive"].Value) == 1)
                {
                    this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
                    this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Black;
                }
                else
                {
                    this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
                    this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.White;
                }
            }
        }

Edit:编辑:

i have now another problem.. Some of the first 1000 records are blacked out.我现在有另一个问题.. 前 1000 条记录中的一些被涂黑了。 I dont know why.我不知道为什么。 I am using the function that i posted here.我正在使用我在此处发布的 function。

Image: picture图片:图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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