首页 文章

使用c#检查gridview是否为空

提问于
浏览
3

目前我有以下内容:

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()
    }
}

我的网格视图如下所示:

enter image description here

但它似乎转到了else语句并发出声音 . 如果gridview的行中没有数据,我需要它才能发出声音 .

2 回答

  • 5

    根据评论,你有:

    dataGridView1.DataSource = BS;
    

    其中BS是 BindingSource ,因此您可以使用其BindingSource.Count属性 .

    所以代码中的某个地方:

    var bindingSource = dataGridView1.DataSource as BindingSource; 
    if(bindingSource.Count == 0) {
      MessageBox.Show("EMPTY");
    }
    
  • 1

    你也可以在填充gridview时检查这个 . 就像这样

    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(); 
    } 
    }
    

相关问题