DataGridView 复选框选择
我将 datagridview
添加到我的 win forms 应用程序中,还添加了一个 CheckBox
用于标记行。 CheckBox
按我的预期工作,直到用户对 DataGridView
进行排序。排序后,之前选择的复选框列将丢失。
有没有办法让我的 datagridview
记住排序后选择了哪一行?
I added datagridview
to my win forms app and I also added one CheckBox
for marking rows.
The CheckBox
works as I expected until the user sorts the DataGridView
. After the sort the previous selection of checkbox column is lost.
Is there a way I can make my datagridview
remember which row is selected after sorting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有两种选择来解决此问题。
第一个也可能是最简单的是将复选框列绑定到数据源。例如,如果您使用 DataTable 作为数据源,则添加布尔列将在 DataGridView 上创建一个复选框,该复选框将排序且不会丢失选中状态。
如果这不是一个选项,那么解决问题的另一种方法是将 DataGridView 设置为虚拟模式并维护复选框值的缓存。
查看精彩的 DataGridView 常见问题解答 有关如何执行此操作的示例。我还提供了下面的代码,但请查看常见问题解答:
You have two options to solve this issue.
The first and possibly the most simple is to bind your checkbox column to your datasource. For example, if you are using a DataTable as your datasource, adding a boolean column will create a checkbox on your DataGridView that will sort and not lose the checked state.
If this is not an option then the other way of addressing the problem is to set your DataGridView to
Virtual
mode and maintain a cache of your checkbox values.Check out the excellent DataGridView FAQ for an example of how to do this. I've also provided the code below but do check out the FAQ:
我很惊讶这种情况发生,但如果在最坏的情况下没有其他方法,您可以将排序设置为编程式,然后在用户单击列标题时进行处理,保存检查项目的列表,执行以编程方式排序,然后检查任何应检查的项目。
I'm surprised that that happens, but if there's no other way around it in a worst case you could set the sorting to programmatic and then handle when the user clicks on the column header, save a list of which items are checked, do the sorting programmatically and then check any items that should be checked.