Alex Terreaux
Alex Terreaux

Reputation: 1941

How can I tell if at least one dataGridView row is selected c#

I have a program that takes a value from the selected row in a dataGridView and passes it to a function. However, the gridView could be empty or could not have a row selected. I took care of the empty Grid, but I was wondering if there is a way if I can tell if any row is selected.

I tried this:

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
}

Do you know how can I solve this?

Upvotes: 3

Views: 6958

Answers (2)

Alex Terreaux
Alex Terreaux

Reputation: 1941

You simply remove the parenthesis after the "Count" keyword. It should look like this:

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
}

Upvotes: 5

Yang You
Yang You

Reputation: 2668

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

Upvotes: 2

Related Questions