Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 803
Alex Terreaux Avatar asked Nov 28 '25 22:11

Alex Terreaux


2 Answers

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
}
like image 143
Alex Terreaux Avatar answered Dec 01 '25 11:12

Alex Terreaux


if (dataGridView1.Rows.Count > 0 && dataGridView1.SelectedRows.Count > 0) {
     ......
}
like image 29
Yang You Avatar answered Dec 01 '25 12:12

Yang You



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!