在DataGridView中对彩色单元格进行计数



我正在研究基于访问的winform应用程序。我在DataGridView中有彩色单元格(DateColumn)。我正在尝试计算PIC中所示的有色单元格,并希望反映标签文本上有色单元格的总数。我尝试了下面的代码,这些代码并未计算我的datagridview的彩色单元格数,尽管总数总行。可以在此图像的帮助下理解确切的问题

我的代码如下:

private void metroGrid1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (this.metroGrid1.Columns[e.ColumnIndex].DataPropertyName == "Date 1")
            try
            {
                var EMIDate1 = Convert.ToDateTime(metroGrid1.Rows[e.RowIndex].Cells["date1DataGridViewTextBoxColumn"].Value);
                for (int i = 0; i < metroGrid1.RowCount; i++)
                {
                    if (EMIDate1 <= DateTime.Today)
                    {
                        int countDarkRed = 0;
                        e.CellStyle.BackColor = Color.DarkRed;
                        e.CellStyle.ForeColor = Color.White;
                        foreach (DataGridViewRow row in this.metroGrid1.Rows)
                        {
                            if (row.Cells["date1DataGridViewTextBoxColumn"].Style.BackColor == Color.DarkRed)
                            {
                                countDarkRed++;
                            }
                        }
                        labelEMI.Text = "Total EMI due as on today:" + countDarkRed;
                    }
                }
            }
            catch
            {
            }
    }

简短答案
您需要在电网的单元格中设置样式,而不仅仅是在单元格的当前格式中。您正在着色错误的牢房。直接访问网格单元格式而不是使用事件单元格,而您的代码将执行您想要的操作(在我的计算机上进行了测试):

        if (EMIDate1 <= DateTime.Today)
        {
            this.metroGrid1[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.DarkRed;
            this.metroGrid1[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.White;
        }

长答案
不要这样做

  • 格式事件将仅应用于可见细胞
    由于控件/窗口太小,因此隐藏了行或列,不会触发此代码
  • 您正在将格式格式(颜色)与业务逻辑(过期日期)混合尝试在绑定网格之前先进行此检查,或者对数据库进行另一个调用
  • 该文档明确表示在单元格式事件中执行太多过程https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.cellformatting?view=netframework-4.7.2#remarks
  • 每次Windows Redraw,Over等时都会执行此代码,使您的标签成为程序中最昂贵的标签之一
    您应该只计算一次

完成工作示例
仅使用一个DataGridView和一个标签

创建一个窗口表单应用程序
    public Form1()
    {
        InitializeComponent();
        dataGridView1.DataSource = new[] { 
            new {Title = "bella", Date1 = DateTime.Now.AddDays(1)}, 
            new {Title = "ciao", Date1 = DateTime.Now.AddDays(12)}, 
            new {Title = "bella", Date1 = DateTime.Now.AddDays(-1)}, 
            new {Title = "ciao", Date1 = DateTime.Now.AddDays(-31)}, 
            new {Title = "bella", Date1 = DateTime.Now.AddDays(11)}, 
            new { Title= "ciao", Date1 = DateTime.Today} ,
            new { Title= "ciao", Date1 = DateTime.Today} ,
            new { Title= "ciao", Date1 = DateTime.Today.AddDays(-7)} };
    }
    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "Date1")
        {
            var date = dataGridView1.Rows[e.RowIndex].Cells["Date1"].Value as DateTime?;
            if (date.HasValue && date.Value <= DateTime.Today)
            {
                dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.DarkRed;
                dataGridView1[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.White;
            }
            int countDarkRed = 0;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells["Date1"].Style.BackColor == Color.DarkRed)
                    countDarkRed++;
            }
            label1.Text = $"dark = {countDarkRed}";
        }
    }

在以前的情况下您出错的地方是,您仅检查第一个单元格并一直在增加计数,现在您将我对上一个问题的评论感到困惑。这是大纲:

有一个如果循环检查日期和更改颜色,一个用于循环的循环计算更改颜色的单元格

private void metroGrid1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (this.metroGrid1.Columns[e.ColumnIndex].DataPropertyName == "Date 1")
    {
        try
        {
            int countDarkRed = 0;
            var EMIDate1 = Convert.ToDateTime(metroGrid1.Rows[e.RowIndex].Cells["date1DataGridViewTextBoxColumn"].Value);
            //Checking whether we have to turn it red or not
            if (EMIDate1 <= DateTime.Today)
            {
                e.CellStyle.BackColor = Color.DarkRed;
                e.CellStyle.ForeColor = Color.White;
            }
            //Checking how many cells have turned red
            foreach(DataGridViewRow row in this.metroGrid1.Rows)
            {
                if (row.Cells["date1DataGridViewTextBoxColumn"].Style.BackColor == Color.DarkRed)
                {
                    countDarkRed++;
                }
            }
            labelEMI.Text = "Total EMI due as on today:" + countDarkRed;
        }
        catch
        {
        }
    }
}

最新更新