Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking if gridview is empty using c#

Currently i have the following:

if (dataGridView1.Rows.Count == 0)
{
    MessageBox.Show("EMPTY");
}
else
{
    using (var soundPlayer = new SoundPlayer(@"c:\Windows\Media\chimes.wav"))
    {
        soundPlayer.Play(); // can also use soundPlayer.PlaySync()
    }
}

My grid view looks like this:

enter image description here

But it seems to go to the else statement and make the sound. I need it to NOT make a sound if there is no data in the rows of the gridview.

like image 655
PriceCheaperton Avatar asked Jun 03 '26 22:06

PriceCheaperton


2 Answers

According to the comment, you have:

dataGridView1.DataSource = BS;

where BS is BindingSource, so you can use its BindingSource.Count property.

So somewhere in the code:

var bindingSource = dataGridView1.DataSource as BindingSource; 
if(bindingSource.Count == 0) {
  MessageBox.Show("EMPTY");
}
like image 113
Tigran Avatar answered Jun 06 '26 12:06

Tigran


You also check this when populate gridview.. like this

DataSet studentsList = students.GetAllStudents(); 
bool empty = IsEmpty(studentsList); 
if(empty) { 
MessageBox.Show("EMPTY"); 
} 
else { 
using (var soundPlayer = new SoundPlayer(@"c:\Windows\Media\chimes.wav"))
{ 
soundPlayer.Play(); 
} 
}
like image 29
S. S. Rawat Avatar answered Jun 06 '26 10:06

S. S. Rawat



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!