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

私のグリッドビューは次のようになります。

ここに画像の説明を入力

しかし、それはelseステートメントに行き、音を出すようです。グリッドビューの行にデータがない場合、音を出さないようにする必要があります。

4

3 に答える 3

5

コメントによると、次のものがあります。

dataGridView1.DataSource = BS;

BS はであるため、そのBindingSource.CountプロパティBindingSourceを使用できます。

したがって、コードのどこかに:

var bindingSource = dataGridView1.DataSource as BindingSource; 
if(bindingSource.Count == 0) {
  MessageBox.Show("EMPTY");
}
于 2013-08-23T07:56:01.447 に答える
0

関数GetCellCount()を使用して、細胞数を取得できます。というパラメーターが必要です。DataGridViewElementStates.Selected

例:

 if (this.myGridView.GetCellCount(DataGridViewElementStates.Selected) > 0)
        {
            //Do sth
        }
        else
        {
            //Show message
        }

利点は、前述の関数を使用して状態を確認するためにデータベース クエリを実行する必要がないことです。詳細: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.getcellcount

于 2019-05-14T12:27:11.850 に答える