DataGridView中选择的行总是0

3
我想在选中的行为0时,显示消息“您未选择任何行”。
但是,即使我点击datagridview中的第一行,并对其进行跟踪,选定的行始终为0。
为什么会这样?
以下是代码:
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);
}
1个回答

6

MSDN上写道:

在SelectionMode属性设置为FullRowSelect或RowHeaderSelect时,才会使用选定行填充SelectedRows属性。

你需要相应地设置这个属性,否则就不会有SelectedRows。


谢谢Steve!问题已解决。但是,这个论坛上的接受按钮还会再激活8分钟,如果我还没有接受你的答案,我很抱歉。 - Kaoru

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接