0

Hello

I'm trying to sort my gridview by a particular colum.

My gridview has a column which is 'Sort Order' and they are numbered 1,2,3 etc.

Now I want to be able to default sort that column in ascending order.

Would anyone possibly be able to help?

Here is my ASP.net code:

<asp:GridView ID="rd" runat="Server" OnPageIndexChanging="pageddata"
            AllowPaging="True" AutoGenerateColumns="False" PageSize="30" CellPadding="4"
            ForeColor="#333333" GridLines="None" AllowSorting="true" OnSorting="onSort">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:TemplateField HeaderText="DataField">
                    <ItemTemplate>
                        <asp:Label ID="lblDataField" runat="server" Text='<%#Eval("DataField") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtDataField" runat="server" ReadOnly="true" Visible="true" Text='<%#Eval("DataField") %>'></asp:TextBox>
                        <%-- Visible=false to hide textbox, ReadOnly=true to make textbox ReadOnly --%>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Header">
                    <ItemTemplate>
                        <asp:Label ID="lblHeader" runat="server" Text='<%#Eval("Header") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtHeader" runat="server" Text='<%#Eval("Header") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="UseDefaults">
                    <ItemTemplate>
                        <asp:Label ID="lblUseDefaults" runat="server" Text='<%#Eval("UseDefaults") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtUseDefaults" runat="server" Text='<%#Eval("UseDefaults") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
               <%-- Taking Sort Order from XML file --%>
                <asp:TemplateField HeaderText="Sort Order" SortExpression="sortOrder">
                <ItemTemplate>
                <asp:Label ID="lblSortOrder" runat="server" Text='<%#Eval("SortOrder") %>'></asp:Label>
                </ItemTemplate>
                
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="True" ButtonType="Button" />
                <asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
            </Columns>
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
            
        </asp:GridView>

The DataBind:

void binddata()
    {
        DataSet rdawTest = new DataSet();
        rdawTest.ReadXml(Server.MapPath(@"xml\" + Request["Gradebook"] + ".xml"));
        foreach (DataColumn da in rdawTest.Tables[0].Columns)
        {
            da.ColumnMapping = MappingType.Attribute;
        }
        rd.DataSource = rdawTest;
        
        rd.DataBind();
    }

Thank you in advance.

Edited by dawsonz: n/a

3
Contributors
5
Replies
7
Views
6 Years
Discussion Span
Last Post by dawsonz
0

if you want to set default ordering then make sorting in query like order by ID asc

0

My code uses an XML file where it collects the data.

I've tried the following code but I get an error:

protected void gridview_Sort(object s,GridViewSortEventArgs e)
    {
        DataSet rdawTest = new DataSet();
        rdawTest.ReadXml(Server.MapPath(@"xml\" + Request["Gradebook"] + ".xml"));
        foreach (DataColumn da in rdawTest.Tables[0].Columns)
        {
            da.ColumnMapping = MappingType.Attribute;
        }
        DataView dv = rdawTest.Tables["table"].DefaultView;
        dv.Sort = "sortOrder ASC";

        rd.DataSource = dv;
        rd.DataBind();
    }

The error:

Object reference not set to an instance of an object

0

Hello

I've tried another approach to my gridview sort, but i'm getting an error hope it's easy enough to resolve.

My code is below:

void binddata()
    {
        DataSet rdawTest = new DataSet();
        rdawTest.ReadXml(Server.MapPath(@"xml\" + Request["Gradebook"] + ".xml"));
        foreach (DataColumn da in rdawTest.Tables[0].Columns)
        {
            da.ColumnMapping = MappingType.Attribute;
        }

            rd.DataSource = rdawTest;
            rd.DataBind();

    }

    protected void gridview_Sort(object sender,GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression; 
        string direction = string.Empty;
        if (SortDirection == SortDirection.Ascending) 
        { 
           SortDirection = SortDirection.Descending; direction = " DESC";
        } 
        else 
        { 
            SortDirection = SortDirection.Ascending; direction = " ASC";
        }
        DataTable table = this.GetData();
        table.DefaultView.Sort = "SortOrder";
        rd.DataSource = table;
        rd.DataBind();
    }

    private DataTable GetData()
    {
        DataTable table = new DataTable();
        DataSet rdawTest = new DataSet();
        rdawTest.ReadXml(Server.MapPath(@"xml\" + Request["Gradebook"] + ".xml"));
        foreach (DataColumn da in rdawTest.Tables[0].Columns)
        {
            da.ColumnMapping = MappingType.Attribute;
        }

        rd.DataSource = rdawTest;
        rd.DataBind();
        return table;
    }

However this is not working as when I click the header to sort it says the following:

Cannot find column SortOrder. at System.Data.DataTable.ParseSortString(String sortString)
at System.Data.DataView.CheckSort(String sort)
at System.Data.DataView.set_Sort(String value)

I'd be grateful if someone could help me rectify this.
Thanks

0

I've now sorted it using my DataBind as follows:

void binddata()
    {
        DataSet rdawTest = new DataSet();
        rdawTest.ReadXml(Server.MapPath(@"xml\" + Request["Gradebook"] + ".xml"));
        foreach (DataColumn da in rdawTest.Tables[0].Columns)
        {
            da.ColumnMapping = MappingType.Attribute;
        }

        System.Data.DataView dv = new System.Data.DataView(rdawTest.Tables[0]);
        dv.Sort = "SortOrder ASC";

            rd.DataSource = dv;
            rd.DataBind();
    }

Thanks for help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.