tags:

views:

1563

answers:

3

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>
+1  A: 

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);
}
Puneet
+1  A: 

If you only want to sort on the radio button click then use the DataView.Sort method e.g.

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

Then on the second radio button click do

dataView.Sort = "Groups, AssetName, Asset, TypeName, Iprisklevel";
Binoj Antony
A: 

You will have to add your columns dynamically. Here is some sample code that add a column that is in turn databound. Note that this Template is a sub class of your page class.

This approach uses a fully templated column. I believe you can also use Bound Columns which might be a bit simpler. All depends on what you typically use in your grid view.

Not tested 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);
    }
}
Andrew Robinson