我们在
Windows窗体中有一个非常奇怪的问题,我们似乎无法弄清楚.
我们的Windows窗体在第一列中有一个带有DataGridViewCheckBoxColumn的DataGridView.
我们添加了以下功能,允许用户切换 – >单击以选择此网格中的多个行:
int colHit = gvLibrary.HitTest(e.X,e.Y).ColumnIndex; int lastRowHit; //mouse left click if (e.Button == MouseButtons.Left) { if (colHit == 0) { if (Control.ModifierKeys == Keys.Shift) { lastRowHit = gvLibrary.HitTest(e.X,e.Y).RowIndex; ShiftClickCheckBoxSetter(this.gvLibrary,int.Parse(txtFirstClickRow.Text),lastRowHit); } else { int firstRowHit = gvLibrary.HitTest(e.X,e.Y).RowIndex; txtFirstClickRow.Text = firstRowHit.ToString(); } } }
private void ShiftClickCheckBoxSetter(DataGridView dataGridView,int p,int lastRowHit) { if (p < lastRowHit) { for (int i = p; i < lastRowHit; i++) { dataGridView.Rows[i].Cells[0].Value = true; } } else// { for (int i = p; i >= lastRowHit; i--) { dataGridView.Rows[i].Cells[0].Value = true; } } }
这是按预期工作的.
我们还为控件添加了一个ContextMenuStrip,用于右键单击事件.
else if (e.Button == MouseButtons.Right) { if (colHit != 0) { ContextMenuStrip m = new ContextMenuStrip(); m.Items.Add("Select All",null,m_LibraryItemClicked); m.Items.Add("Select None",m_LibraryItemClickednone); m.Show(gvLibrary,e.Location); } }
代表活动一:
void m_LibraryItemClicked(object sender,EventArgs e) { foreach (DataGridViewRow dgvr in gvLibrary.Rows) { if (dgvr.Selected) { dgvr.Selected = false; } dgvr.Cells["LSelect"].Value = true; } }
代表事件二:
private void m_LibraryItemClickednone(object sender,EventArgs e) { foreach (DataGridViewRow dgvr in gvLibrary.Rows) { if (dgvr.Selected) dgvr.Selected = false; dgvr.Cells["LSelect"].Value = false; } }
这允许用户选择全部或选择无复选框.
选择“全选”选项后,将选中所有复选框:
但是,选择“选择无”选项时:
取消选中所有复选框,但在Shift-Click事件中选中的最后一个复选框除外:
我认为迭代所有Grid Rows并将复选框设置为未选中就足够了,IE:
private void m_LibraryItemClickednone(object sender,EventArgs e) { foreach (DataGridViewRow dgvr in gvLibrary.Rows) { if (dgvr.Selected) dgvr.Selected = false; dgvr.Cells["LSelect"].Value = false; } }
提前致谢.
解决方法
我检查了你的代码,可以重现这种行为.问题似乎与当前单元格(不是选定的单元格)有关.当您尝试更改此特定单元格时,操作不会立即执行.
要更改此行为,请添加dataGridView1.CurrentCell = null;在更改“LSelect”单元格的值之前.这应该可以解决您的问题.
private void m_LibraryItemClickednone(object sender,EventArgs e) { dataGridView1.CurrentCell = null; foreach (DataGridViewRow dgvr in gvLibrary.Rows) { if (dgvr.Selected) dgvr.Selected = false; dgvr.Cells["LSelect"].Value = false; } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。