目前,我有以下几点:
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()
}
}我的网格视图如下所示:

但它似乎是去其他的陈述,并发出声音。如果网格视图的行中没有数据,我需要它不发出声音。
发布于 2013-08-23 07:56:01
根据评论,你有:
dataGridView1.DataSource = BS;其中BS是BindingSource,所以您可以使用它的BindingSource.Count属性。
所以在代码中的某个地方:
var bindingSource = dataGridView1.DataSource as BindingSource;
if(bindingSource.Count == 0) {
MessageBox.Show("EMPTY");
}发布于 2013-08-23 08:05:44
在填充网格视图时也会检查这个。像这样
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();
}
}发布于 2019-05-14 12:27:11
您可以使用函数GetCellCount()来获取单元格数。它需要一个名为DataGridViewElementStates.Selected的参数
示例:
if (this.myGridView.GetCellCount(DataGridViewElementStates.Selected) > 0)
{
//Do sth
}
else
{
//Show message
}优点是不需要运行数据库查询来使用上述函数检查条件。更多细节:https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.getcellcount
https://stackoverflow.com/questions/18397586
复制相似问题