Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selected Rows in DataGridView always 0

i wanted to show the message "You have not selected any rows" if the selected rows is 0.

But, even i click the first row in datagridview, and i trace it, the selected rows always gave me 0.

Why is it like that?

Here is the code:

MessageBox.Show(dataGridView1.SelectedRows.Count.ToString()); // below function always run, because the selected rows always 0, even i clicked the first row in datagridview (when run the program)

if (dataGridView1.SelectedRows.Count == 0)
{
    if (choice.comboBox1.Text == "English")
    {
        System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
        sound.Play();
        MessageBox.Show("You Have Not Selected Any Rows!", "Error");
    }
}

else
{
    DeleteDatabase(sender, e);
}
like image 845
Kaoru Avatar asked Mar 29 '26 22:03

Kaoru


1 Answers

The MSDN says

The SelectionMode property must be set to FullRowSelect or RowHeaderSelect for the SelectedRows property to be populated with selected rows.

You need to set this property accordingly otherwise no SelectedRows.

like image 121
Steve Avatar answered Apr 02 '26 14:04

Steve



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!