如何判断至少一个dataGridView行被选中 c#

3

我有一个程序,它从dataGridView中选择的行中获取一个值并将其传递给函数。然而,gridView可能为空或没有选择任何行。

我已经处理了空Grid,但我想知道是否有一种方法可以判断是否选择了任何行。

我尝试了这个:

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
}

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


没事了,我已经解决了,问题出在第二个“Count”后面的括号,谢谢。 - Alex Terreaux
2个回答

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
}

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接