我需要以编程方式在datagridview控件中选择一列 [英] I need to select a column in a datagridview control programatically

查看:75
本文介绍了我需要以编程方式在datagridview控件中选择一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了这个论坛和其他许多人,但无法正确制定我的搜索问题。



我有一个datagridview(VS2013,.Net4,VB)加载有数据。我需要将其设置为以编程方式对列1数据进行排序。 Microsoft On-line帮助显示如何使用用户选择的列进行排序,但不会以编程方式选择任何列时如何选择列。



首先,它甚至可能吗?



其次,如果你知道如何做到这一点我会非常感激。

I have searched this forum and many others but am unable to correctly formulate my search question.

I have a datagridview (VS2013, .Net4, VB) loaded with data. I need to set it to sort the column 1 data programmatically. The Microsoft On-line help shows how to sort using a user-selected column, but not how to select a column when none are selected, programmatically.

First, it is even possible ?

Second, if you know how this might be done I would appreciate it greatly.

推荐答案

以下是为此网格视图类实现的选择模式:

https://msdn.microsoft.com/en-us/library/3c89df86%28v=vs.110%29.aspx [ ^ ]。



这是用于设置所选选择模式的属性: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectionmode%28v=vs.110 %29.aspx [ ^ ]。



-SA
Here are the selection modes implemented for this grid view class:
https://msdn.microsoft.com/en-us/library/3c89df86%28v=vs.110%29.aspx[^].

And this is the property used to set chosen selection mode: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectionmode%28v=vs.110%29.aspx[^].

—SA


请阅读: DataGridView:如何选择整个列并取消选择其他所有列? [ ^ ]。听起来你想要选择整列进行排序,但是datagridview仍然使用 SortMode = DataGridViewColumnSortMode.Automatic 。根据我的理解,你必须禁用自动排序。
Please, read this: DataGridView: How to select an entire Column and deselect everything else?[^]. Sounds like you want to select entire column to sort it, but a datagridview is still using SortMode = DataGridViewColumnSortMode.Automatic. As per my understanding, you have to disable automatic sorting.


谢谢你的回复。我看了你的建议,他们都可以工作。与此同时,我一直在关注这个问题并意识到我正在过度思考(再次!:()!)问题。



对我有用的解决方案是如下:

Thanks guys for your replies. I have looked at your suggestions, and they both could work. In the meantime, I kept plugging away at the issue and realize I was overthinking (Again!:()!) the issue.

The solution that works for me is as follows:
my_data_grid.Sort(my_data_grid.Columns(1), System.ComponentModel.ListSortDirection.Ascending)

这篇关于我需要以编程方式在datagridview控件中选择一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆