如何判断是否至少选择了一个dataGridView行c#

时间:2012-12-03 02:32:40

标签: c# .net winforms datagridview

我有一个程序从dataGridView中的选定行获取值并将其传递给函数。但是,gridView可能为空或无法选择行。 我处理了空网格,但我想知道是否有办法可以判断是否有任何行被选中。

我试过了:

if (Convert.ToInt32(dataGridView1.Rows.Count) > 0)
{
    //It is not empty
}
int c = dataGridView1.SelectedRows.Count(); //this line gives me an error
if (c>0)
{
    //there is a row selected
}

你知道我怎么解决这个问题?

2 个答案:

答案 0 :(得分:5)

您只需删除“Count”关键字后的括号即可。它应该是这样的:

if (Convert.ToInt32(dataGridView1.Rows.Count) > 0)
{
    //It is not empty
}
int c = dataGridView1.SelectedRows.Count; //remove parenthesis here
if (c>0)
{
    //there is a row selected
}

答案 1 :(得分:2)

if (dataGridView1.Rows.Count > 0 && dataGridView1.SelectedRows.Count > 0) {
     ......
}