我如何找出哪些项目? DataGridView 中的选定状态已更改?

发布于 2024-08-14 09:03:47 字数 184 浏览 6 评论 0原文

我有一个打开了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌路黄昏 2024-08-21 09:03:47

该事件并没有确切地告诉您哪些事情发生了变化。如果您出于某种原因需要知道,则必须跟踪之前的选择。

针对这一事件,您打算采取什么措施?可能有一种更简单的方法来实现您的真正目标。

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.

只为守护你 2024-08-21 09:03:47

该信息应该位于事件参数中。

使用 RowStateChanged 事件。 DataGridViewRowStateChangedEventArgs 将包含被单击的行。如果用户选择/取消选择多行,则为选择/取消选择的每一行调用该事件一次。

e.Row.Selected 将产生该行现在是被选择还是被取消选择。

That information should be in the event arguments.

Use the RowStateChanged event. the DataGridViewRowStateChangedEventArgs 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.

淡淡離愁欲言轉身 2024-08-21 09:03:47

此信息本质上不适用于 DataGridView。但是,您可以在提供此信息的 DataGridView 周围编写一个包装器。

public static void OnSelectionChanged(
  this DataGridView view,
  Action<List<DataGridViewRow>,List<DataGridViewRow>> handler) {
  var oldSelection = view.SelecetedRows.Cast<DataGridViewRow>.ToList();
  view.SelectedChanged += (sender,e)  {
    var newSelection = view.SelectedRows.Cast<DataGridViewRow>.ToList();
    handler(oldSelection,newSelection);
    oldSelection = newSelection;
  };
}

使用案例

void HandleSelectionChanged(List<DataGridViewRow> oldRows, List<DataGridViewRow> newRows) {
  ..
}

void FormLoaded() {
  myDataGridView.OnSelectionChanged(HandleSelectionChanged);
}

This information is not inherently available for a DataGridView. However you could write a wrapper around the DataGridView which provides this information.

public static void OnSelectionChanged(
  this DataGridView view,
  Action<List<DataGridViewRow>,List<DataGridViewRow>> handler) {
  var oldSelection = view.SelecetedRows.Cast<DataGridViewRow>.ToList();
  view.SelectedChanged += (sender,e)  {
    var newSelection = view.SelectedRows.Cast<DataGridViewRow>.ToList();
    handler(oldSelection,newSelection);
    oldSelection = newSelection;
  };
}

Use Case

void HandleSelectionChanged(List<DataGridViewRow> oldRows, List<DataGridViewRow> newRows) {
  ..
}

void FormLoaded() {
  myDataGridView.OnSelectionChanged(HandleSelectionChanged);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文