How can I dynamically reorder GridView columns in WPF?

I have two radio blocks. If I hit the first radiator, I want the column order to be as follows:

  • Assetname
  • Asset
  • Groups
  • Typename
  • Iprisklevel

If I click on the second switch, I want the column order to be:

  • Groups
  • Assetname
  • Asset
  • Typename
  • Iprisklevel

Here is an example 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>
+3
source share
3 answers

Instead of adding columns in design mode, you might have the idea of ​​adding them dynamically using C # code. Which will allow you to order them the 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);
}
+2
source

If you only want to sort by clicking the switch, use the DataView.Sort method, for example.

dataView.Sort = "AssetName, Asset, Groups, TypeName, Iprisklevel";

dataView.Sort = "Groups, AssetName, Asset, TypeName, Iprisklevel";
+1

You will have to dynamically add your columns. Here is an example of code that adds a column, which in turn binds to a database. Note that this template is a subclass of your class.

This approach uses a fully boilerplate column. I believe that you can also use Bound Columns, which can be a little easier. It all depends on what you usually use as a grid.

Not verified, but cut from working code:

private void AddTemplates()
{
    TemplateField templateField = new TemplateField();
    templateField.HeaderText = entity.ChangeHistoryColumn;
    templateField.ItemTemplate = new GridViewColumnTemplate();
    GridViewMain.Columns.Add(templateField);
}

public class GridViewColumnTemplate : ITemplate
{
    public GridViewColumnTemplate() { }

    public void InstantiateIn(Control container)
    {
        Label label = new Label();
        label.DataBinding += delegate(object sender, EventArgs e)
        {
            GridViewRow row = (GridViewRow)label.NamingContainer;

            int headerID = (int)DataBinder.Eval(row.DataItem, "HeaderID");
            ((Label)sender).Text = headerID.ToString();
        };

        container.Controls.Add(label);
    }
}
0
source

Source: https://habr.com/ru/post/1705652/


All Articles