选择最后一行时,Datagridview SelectedRows.Count始终为零 [英] Datagridview SelectedRows.Count is always zero when selecting last row
问题描述
我有一个datagridview,在其中我已将允许用户添加行属性设置为false.
I have a datagridview where I have set the allow user to add row property to false.
我还使得只能在datagridview上进行fullrowselect.
I have also made it only possible to have a fullrowselect on the datagridview.
用户通过按工具栏上的"+"按钮将行添加到数据网格.DGV是通过将数据源拖动到添加了绑定导航器工具条的表单中创建的.
A user adds rows to the datagrid by pressing the "+" button of the toolstrip. The DGV was created by dragging a datasourse to a form that added the binding navigator toolstrip.
我的问题是,在我的行删除代码中,我检查了
My problem is that in my code for row deletion, i check
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
{
try
{
if (dgvUsers.SelectedRows.Count > 0 && dgvUsers.Rows[0].Selected)
MessageBox.Show("You cannot remove the user admin");
else
{
if (MessageBox.Show("Are you sure you want to remove the user?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
int code = 0;
UserService userService = new UserService();
if (dgvUsers.SelectedRows.Count > 0)
{
int selectedrowindex = dgvUsers.SelectedCells[0].RowIndex;
DataGridViewRow selectedRow = dgvUsers.Rows[selectedrowindex];
code = (int)(selectedRow.Cells[0].Value);
if (GlobalClass.SessionId == "admin")
{
userService.RemoveUsers(code);
}
else
MessageBox.Show("Only username 'admin' can remove users");
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
当我选择要删除的最后一行时,SelectedRows.Count =0.但是当我选择任何其他行时,它为"1".为什么会这样?
When I select the last row for deletion, SelectedRows.Count = 0. But it is "1" when I select any other row. Why is this?
推荐答案
可以检查"SelectionMode"属性是否设置为FullRowSelect吗?
Can you check if the "SelectionMode" property is set to FullRowSelect?
在MSDN中写道:
必须将SelectionMode属性设置为FullRowSelect或RowHeaderSelect,才能使用选定的行填充SelectedRows属性."
"The SelectionMode property must be set to FullRowSelect or RowHeaderSelect for the SelectedRows property to be populated with selected rows."
这篇关于选择最后一行时,Datagridview SelectedRows.Count始终为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!