我如何可以动态地改变我的WPF GridView中列的顺序? [英] How can I dynamically change the order of my GridView columns in WPF?

查看:127
本文介绍了我如何可以动态地改变我的WPF GridView中列的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个单选按钮。 如果我点击第一个单选按钮我想列的顺序是:

  • ASSETNAME
  • 资产
  • 类型名
  • Iprisklevel

如果我点击第二个单选按钮,我想列的顺序是:

  • ASSETNAME
  • 资产
  • 类型名
  • Iprisklevel

下面是我的XAML的示例:

 < ASP:GridView控件ID =dgAssets=服务器的AutoGenerateColumns =FALSEAllowPaging =真
            DataKeyNames =IDAllowSorting =真OnPageIndexChanging =dgAssets_PageIndexChanging
            宽度=100%OnRowCommand =dgAssets_RowCommandOnRowDataBound =dgAssets_RowDataBound
            OnSorting =dgAssets_Sorting>
            <列>

                < ASP:的TemplateField可见=假>
                    <的ItemTemplate>
                        &所述;%#的DataBinder.Eval(的Container.DataItem,ID)%>
                    < / ItemTemplate中>
                < / ASP:的TemplateField>
                < ASP:的TemplateField SORTEX pression =ASSETNAME>
                    <的ItemTemplate>
                        <%#的DataBinder.Eval(的Container.DataItem,ASSETNAME)%>
                    < / ItemTemplate中>
                < / ASP:的TemplateField>
                < ASP:的TemplateField SORTEX pression =资产>
                    <的ItemTemplate>
                        <%#的DataBinder.Eval(的Container.DataItem,资产)%>
                    < / ItemTemplate中>
                < / ASP:的TemplateField>
                < ASP:的TemplateField SORTEX pression =组>
                    <的ItemTemplate>
                        <%#的DataBinder.Eval(的Container.DataItem,组)%>
                    < / ItemTemplate中>
                < / ASP:的TemplateField>
                < ASP:的TemplateField SORTEX pression =类型名称>
                    <的ItemTemplate>
                        <%#的DataBinder.Eval(的Container.DataItem,类型名称)%>
                    < / ItemTemplate中>
                < / ASP:的TemplateField>
                < ASP:的TemplateField SORTEX pression =IPRISKLEVEL>
                    <的ItemTemplate>
                        <%#的DataBinder.Eval(的Container.DataItem,IPRISKLEVEL)%>
                    < / ItemTemplate中>
                < / ASP:的TemplateField>

            < /列>
        < / ASP:GridView控件>
 

解决方案

而不是增加在设计模式中的列,它可能是一个想法,动态地使用C#code添加它们。这将允许你命令他们任何你喜欢的方式。

$ C $下动态地添加列:

  DataTable的DT = yourDataTable;
的foreach(COL的DataColumn在dt.Columns)
{
    绑定列bfield =新的绑定列();
    bfield.DataField = col.ColumnName;
    bfield.HeaderText = col.ColumnName;
    dgAssets.Columns.Add(bfield);
}
 

I have two radiobuttons. If I click the first radiobutton I want the column's order to be:

  • AssetName
  • Asset
  • Groups
  • TypeName
  • Iprisklevel

If I click the second radio button I want the column's order to be:

  • Groups
  • AssetName
  • Asset
  • TypeName
  • Iprisklevel

Here is a sample of my XAML:

<asp:GridView ID="dgAssets" runat="server" AutoGenerateColumns="False" AllowPaging="True"
            DataKeyNames="ID" AllowSorting="True" OnPageIndexChanging="dgAssets_PageIndexChanging"
            Width="100%" OnRowCommand="dgAssets_RowCommand" OnRowDataBound="dgAssets_RowDataBound"
            OnSorting="dgAssets_Sorting">
            <Columns>

                <asp:TemplateField Visible="False">
                    <ItemTemplate>
                        <%# DataBinder.Eval(Container.DataItem, "ID")%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField SortExpression="ASSETNAME">
                    <ItemTemplate>
                        <%# DataBinder.Eval(Container.DataItem, "ASSETNAME")%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField SortExpression="ASSET">
                    <ItemTemplate>
                        <%# DataBinder.Eval(Container.DataItem, "ASSET")%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField SortExpression="GROUPS">
                    <ItemTemplate>
                        <%# DataBinder.Eval(Container.DataItem, "GROUPS")%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField SortExpression="TYPENAME">
                    <ItemTemplate>
                        <%# DataBinder.Eval(Container.DataItem, "TYPENAME")%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField SortExpression="IPRISKLEVEL">
                    <ItemTemplate>
                        <%# DataBinder.Eval(Container.DataItem, "IPRISKLEVEL")%>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>
        </asp:GridView>

解决方案

Instead of adding the columns in design mode, it might be an idea to add them dynamically using C# code. Which will allow you to order them any way you like.

Code for dynamically adding columns:

DataTable dt = yourDataTable;
foreach (DataColumn col in dt.Columns)
{
    BoundField bfield = new BoundField();
    bfield.DataField = col.ColumnName;
    bfield.HeaderText = col.ColumnName;
    dgAssets.Columns.Add(bfield);
}

这篇关于我如何可以动态地改变我的WPF GridView中列的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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