2012-12-03 35 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) { 
    ...... 
} 
相關問題