C#:将 DataTable 绑定到 GridView 时更改列的顺序 [英] C#: Changing the order of columns when binding DataTable to a GridView

查看:52
本文介绍了C#:将 DataTable 绑定到 GridView 时更改列的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改 DataTable 中列的显示顺序?

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#:将 DataTable 绑定到 GridView 时更改列的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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