我有一个datagridview,我已经设置allow user将row属性添加到false.
我还让它只能在datagridview上进行全视图选择.
用户通过按工具条的"+"按钮将行添加到数据网格.通过将数据拖动到添加了绑定导航器工具条的表单来创建DGV.
我的问题是在我的行删除代码中,我检查
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);
}
}
Run Code Online (Sandbox Code Playgroud)
当我选择最后一行进行删除时,SelectedRows.Count = 0.但是当我选择任何其他行时它为"1".为什么是这样?
小智 6
你能检查一下"SelectionMode"属性是否设置为FullRowSelect?
在MSDN中写道:
"SelectionMode属性必须设置为FullRowSelect或RowHeaderSelect,以便SelectedRows属性填充选定的行."