C#中:数据表绑定到GridView时更改列的顺序 [英] C#: Changing the order of columns when binding DataTable to a GridView
问题描述
这怎么可能更改列从数据表显示的顺序?
How is it possible to change the displayed order of columns from a DataTable?
例如,dataTable的DT包含两列的a和b。我把它绑定到这样一个GridView:
For example, dataTable "dt" contains two columns "a" and "b". I bind it to a GridView like this:
gridView.DataSource = dt;
gridView.DataBind();
但我想在GridView显示B第一个(最左边)。
But I'd like the GridView to display "b" first (leftmost).
重要的一点:我使用这个导出到Excel,而且也没有实际输出到屏幕,使用:
Important point: I'm using this to export to Excel and there's no actual output to screen, using:
HtmlTextWriter htw = new HtmlTextWriter(sw);
gridView.RenderControl(htw);
谢谢!
推荐答案
是的,你可以做到这一点在前端。沿着这些路线的内容:
yes you can do it in the front end. Something along these lines:
<asp:GridView runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="b" />
<asp:BoundField DataField="a" />
</Columns>
</asp:GridView>
编辑:
没有前端的你说什么?这很酷 - 我喜欢挑战:
No front end you say? That's cool - I like a challenge:
gridView.AutoGenerateColumns = false;
gridView.Columns.Add(new BoundField { DataField = "b" });
gridView.Columns.Add(new BoundField { DataField = "a" });
(这很酷假设C#3,这些天是不是?)
(It's cool to assume C#3 these days isn't it?)
这篇关于C#中:数据表绑定到GridView时更改列的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!