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

Ale*_*aux 3 .net c# datagridview winforms

我有一个程序从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
}
Run Code Online (Sandbox Code Playgroud)

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

Ale*_*aux 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
}
Run Code Online (Sandbox Code Playgroud)