C# 如何动态重新排序gridview中的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8976153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:25:07  来源:igfitidea点击:

How to reorder columns in gridview dynamically

c#asp.net.netgridview

提问by Ragalante

Similar questions may exists, but none of them seems to be helpfull. So I'll try to explain a more specific case, and see if anyone can help me :)

可能存在类似的问题,但它们似乎都没有帮助。所以我会尝试解释一个更具体的案例,看看是否有人可以帮助我:)

Here is the thing, I have a gridview with templatefields, and I want to let the user to specify the order those columns are shown. So, the user creates a view and decides which column to display first, second, and so on.

事情就是这样,我有一个带有模板字段的 gridview,我想让用户指定这些列的显示顺序。因此,用户创建一个视图并决定首先显示哪一列,第二列等等。

So basically, I need to change the order of the columns just after the grid is loaded with data.

所以基本上,我需要在网格加载数据后更改列的顺序。

Sounds easy huh? Well, apparently it's not. At least I couldn't achieve that just yet.

听起来很容易吧?嗯,显然不是。至少我现在还做不到。

Some notes: - Of course I have AutogenerateColumns set to false. - Change the sql select columns order won't work because of previous item. - And I'd like not to generate the columns by the code.

一些注意事项: - 当然,我将 AutogenerateColumns 设置为 false。- 由于前一项,更改 sql 选择列顺序将不起作用。- 我不想通过代码生成列。

Any ideas?

有任何想法吗?

采纳答案by patmortech

You can modify the Gridview's Columns collection in your code-behind. So, one way of doing this is to remove the column from its current position in the collection and then re-insert it into the new position.

您可以在代码隐藏中修改 Gridview 的 Columns 集合。因此,执行此操作的一种方法是将列从集合中的当前位置移除,然后将其重新插入到新位置。

For example, if you wanted to move the second column to be the first column you could do:

例如,如果您想将第二列移动到第一列,您可以执行以下操作:

var columnToMove = myGridView.Columns[1];
myGridView.Columns.RemoveAt(1);
myGridView.Columns.Insert(0, columnToMove);

If you need to move them all around randomly, then you might want to try to clone the field collection, clear the collection in the GridView, and then re-insert them all in the order you want them to be in.

如果您需要随机移动它们,那么您可能需要尝试克隆字段集合,清除 GridView 中的集合,然后按照您希望它们的顺序重新插入它们。

var columns = myGridView.Columns.CloneFields();
myGridView.Columns.Clear();
myGridView.Columns.Add(columns[2]);
myGridView.Columns.Add(columns[0]);
etc..

I'm not 100% sure whether this all will work AFTER binding to data, so unless there's a reason not to, I'd do it in Page_Init or somewhere before binding.

我不是 100% 确定这一切是否会在绑定到数据后工作,所以除非有理由不这样做,否则我会在 Page_Init 或绑定前的某个地方进行。

回答by Steven

I know this is an really old question but the suggested answer did not solve the problem fully.

我知道这是一个非常古老的问题,但建议的答案并没有完全解决问题。

The ViewState for the GridView will failed after remove / add a dynamic column.

删除/添加动态列后,GridView 的 ViewState 将失败。

If you need to bind any event, like OnRowCommand and OnRowUpdating, such events will not be fired.

如果您需要绑定任何事件,例如 OnRowCommand 和 OnRowUpdating,则不会触发此类事件。

Edit:

编辑:

Look into this function if you need answers.

如果您需要答案,请查看此功能。

Protected Overrides Function CreateColumns(dataSource As PagedDataSource, useDataSource As Boolean) As ICollection