我如何找出哪些项目? DataGridView 中的选定状态已更改?
我有一个打开了 MultiSelect 的 DataGridView。当 SelectionChanged 事件被触发时,我想知道新选择了哪些项目以及新取消选择了哪些项目。例如,如果您选择了多个项目(通过按住 Ctrl 键单击),然后释放 Ctrl 键并选择单个项目,我想知道哪些项目被取消选择。我可以跟踪以前选择的项目集合,但我只是想确保我没有想太多。
I have a DataGridView that has MultiSelect turned on. When the SelectionChanged event is fired, I'd like to know what items were newly selected and which items were newly deselected. For instance, if you have multiple items selected (by Ctrl-clicking) and then you release the Ctrl key and select a single item, I'd like to know which items were deselected. I could keep track of the previous selected items collection, but I just wanted to make sure I wasn't thinking too hard.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该事件并没有确切地告诉您哪些事情发生了变化。如果您出于某种原因需要知道,则必须跟踪之前的选择。
针对这一事件,您打算采取什么措施?可能有一种更简单的方法来实现您的真正目标。
The event does not tell you exactly which things changed. If you need to know for some reason, you will have to keep track of the previous selection.
What are you trying to do in response to this event? There may be a much easier way to accomplish your real goal.
该信息应该位于事件参数中。
使用
RowStateChanged
事件。DataGridViewRowStateChangedEventArgs
将包含被单击的行。如果用户选择/取消选择多行,则为选择/取消选择的每一行调用该事件一次。e.Row.Selected
将产生该行现在是被选择还是被取消选择。That information should be in the event arguments.
Use the
RowStateChanged
event. theDataGridViewRowStateChangedEventArgs
will contain the row that was clicked. If a user selects/deselects multiple rows, the event will be called once for each row selected/deselected.e.Row.Selected
will yield whether the row is now selected or deselected.此信息本质上不适用于
DataGridView
。但是,您可以在提供此信息的 DataGridView 周围编写一个包装器。使用案例
This information is not inherently available for a
DataGridView
. However you could write a wrapper around theDataGridView
which provides this information.Use Case