2012-12-03 33 views
3

我有一个程序,它从dataGridView中的选定行获取值并将其传递给函数。但是,gridView可能是空的或不能选择一行。 我照顾了空格,但我想知道是否有方法可以判断是否选择了任何行。我如何知道是否至少选择了一个dataGridView行c#

我尝试这样做:

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 
} 

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

+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 
} 
2
if (dataGridView1.Rows.Count > 0 && dataGridView1.SelectedRows.Count > 0) { 
    ...... 
} 
相关问题