如何计算datagridview中的彩色单元格? [英] How to count colored cell in datagridview?
本文介绍了如何计算datagridview中的彩色单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发基于Access的WinForm应用程序。我在DataGridView中有彩色单元格(DateColumn)。我正在尝试计算这些细胞并在标签文本上反映有色细胞编号。我的代码如下:
I am working on access based WinForm application. I have colored cell(DateColumn) in my DataGridView. I am trying to count these cell and reflect colored cell No. on a label text. My codes are as follows:
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);
if (EMIDate1 <= DateTime.Today)
{
e.CellStyle.BackColor = Color.DarkRed;
e.CellStyle.ForeColor = Color.White;
int countDarkRed = 0;
for (int i = 0; i < metroGrid1.RowCount; i++)
{
if (e.CellStyle.BackColor == Color.DarkRed)
++countDarkRed;
labelEMI.Text = "Total EMI due as on today:" + countDarkRed;
}
}
}
catch
{
}
}
我尝试过:
我尝试了上面的代码,但是每当添加新行时,这些代码都会计算DataGridView的行数。它会增加no。 1.
What I have tried:
I tried above code but these codes are counting DataGridView's rows whenever new row is added.It increases no. by 1.
推荐答案
试试这个:
Try this:
labelEMI.Text = string.Format("Total EMI due as on today: {0}", metroGrid1.Rows.Cast<DataGridViewRow>()
.Count(r=>r.Cells["Date 1"].Style.BackColor == Color.DarkRed));
这篇关于如何计算datagridview中的彩色单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文