根据C#中的单元格值获取行数

时间:2017-08-11 09:16:14

标签: c# winforms datagridview

所以我试图从DataGridView获取行数,具体取决于它的值。在MessageBox中,应显示此值在DataGridView上的所有行数。

这是我得到的代码:

foreach (DataGridViewRow row in dataGridView1.Rows)
{   
    if (row.Cells[1].Value.ToString().Trim() == "4 - Urgent")
    {
        MessageBox.Show("You have things to do!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

如何根据"4 - Urgent"值获取行数?

3 个答案:

答案 0 :(得分:1)

试试这个:

array('bla', 'foo', 'baz' => 2)

答案 1 :(得分:0)

您可以尝试以下方式:

 int count = dataGridView1.Rows.Cast<DataGridViewRow>().Where(r=> r.Cell[1].Value.Tostring.Trim()== "4 - urgent").Count();

答案 2 :(得分:0)

int totalCount = 0;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
    if (dg.Rows[i].Cells[1].Value.ToString().Trim() == "4 - Urgent") 
   //Trim() for removing whitespaces
    {
         totalCount += 1;
         MessageBox.Show(i); // this will give you the row index
    }
}
MessageBox.Show(totalCount.ToString()); //Total no of rows containing  "4 - Urgent"