[英]Datagridview SelectedRows.Count is always zero when selecting last row
我有一個datagridview,在其中我已將允許用戶添加行屬性設置為false。
我還使在datagridview上只有fullrowselect成為可能。
用戶可以通過按下工具欄的“ +”按鈕將行添加到數據網格。 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);
}
}
當我選擇要刪除的最后一行時,SelectedRows.Count =0。但是當我選擇任何其他行時,它為“ 1”。 為什么是這樣?
您可以檢查“ SelectionMode”屬性是否設置為FullRowSelect嗎?
在MSDN中寫道:
“必須將SelectionMode屬性設置為FullRowSelect或RowHeaderSelect,才能使用選定的行填充SelectedRows屬性。”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.