0

I have created web form using master page in my Web application.And i have created GridView and i want to sort the columns.My gridview design as follows:

<div id="GridPopUp" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false"  DataKeyNames="sno"  CssClass = "grid" >
    <Columns>
        <asp:TemplateField  ItemStyle-Width="200px">
            <ItemTemplate>
                <asp:CheckBox  ID="cbCheck" runat="server"/>
            </ItemTemplate>
            </asp:TemplateField>

        <asp:BoundField ItemStyle-Width="200px" DataField="sno" HeaderText="sno"  />
        <asp:BoundField ItemStyle-Width="200px" DataField="itemcode" HeaderText="ItemCode" />
        <asp:BoundField ItemStyle-Width="200px" IDataField="itemname" HeaderText="ItemName"  />
        <asp:BoundField ItemStyle-Width="200px" DataField="unit" HeaderText="Unit"  />
        <asp:BoundField ItemStyle-Width="200px" DataField="price" HeaderText="Price"  />
        <asp:BoundField ItemStyle-Width="200px" DataField="qty" HeaderText="Qty"  />
         <asp:TemplateField ItemStyle-Width="200px" >
            <ItemTemplate>
                <asp:LinkButton ID="lnkSelect" runat="server" Text="Select" CommandName = "Select" OnClientClick = "return GetSelectedRow(this)" />
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>
</div>
7
  • Does this help you? stackoverflow.com/a/10498012/1166719 Commented Dec 9, 2016 at 12:34
  • just enable sorting on the gridview Commented Dec 9, 2016 at 16:44
  • I already did that allowsorting=true; but it shows error message Commented Dec 9, 2016 at 16:55
  • I already did that AllowSorting="True". but it shows error message "The GridView 'GridView1' fired event Sorting which wasn't handled."I defined SortExpression="sno" and so on. Commented Dec 9, 2016 at 17:08
  • Thanks for your reply Pierre-Loup Pagniez and fnostro. your suggested thread is work for me.i would like to do this in without postback with help of javascrip and jquerry Commented Dec 9, 2016 at 17:32

1 Answer 1

0

I did sorting a column in Gridview with the help of thread available here

my project coding is as follows,

<asp:GridView ID="GvSalesItems" runat="server"  AutoGenerateColumns="false" DataKeyNames="sno" CellPadding="4" ForeColor="#333333" GridLines="None"  PageSize="10" AllowPaging="True" AllowSorting="True" onsorting="GridView1_Sorting" EnableViewState="true">
           <Columns>
       <asp:TemplateField>

    <ItemTemplate>
      <asp:CheckBox ID="chk" runat="server" />
    </ItemTemplate>
</asp:TemplateField>
 <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkSelect" runat="server" Text="Select" CommandName = "Select" OnClientClick = "return GetSelectedRow(this)" />
            </ItemTemplate>
        </asp:TemplateField>

              <asp:BoundField DataField="sno" HeaderText="Sno"  SortExpression="sno" />
            <asp:BoundField DataField="itemcode" HeaderText="Item Code"  SortExpression="itemcode" />
            <asp:BoundField DataField="itemname" HeaderText="Item Name"  SortExpression="itemname" />
            <asp:BoundField DataField="unit" HeaderText="Unit"  SortExpression="unit" />
            <asp:BoundField DataField="price" HeaderText="Price"  SortExpression="price" />
            <asp:BoundField DataField="default_qty" HeaderText="Qty"  ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol"  />
            <asp:BoundField DataField="price" HeaderText="Amount" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol"  />


        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>

Code behind is,

 public void GetSalesItems() //Binding GridView
        {
                DataSet ds1 = new DataSet();
                D.id = 2;//SELECT * from DBTABLE
                ds1 = C.DATA1(D);
                GvSalesItems.DataSource = ds1.Tables[0];
                GvSalesItems.DataBind();

        }

Codings for Sorting a GridView using ASP.net Buildin sorting functions,

 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
            {

                DataSet ds = new DataSet();
                B.id = 2;
                ds = A.DATA(B);
                GvUser.DataSource = ds.Tables[0];
                GvUser.DataBind();

                if (ds.Tables[0] != null)
                {
                    DataView dataView = new DataView(ds.Tables[0]);
                    dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

                    GvUser.DataSource = dataView;
                    GvUser.DataBind();
                }
            }

            private string GridViewSortDirection
            {
                get { return ViewState["SortDirection"] as string ?? "DESC"; }
                set { ViewState["SortDirection"] = value; }
            }

            private string ConvertSortDirectionToSql(SortDirection sortDirection)
            {
                switch (GridViewSortDirection)
                {
                    case "ASC":
                        GridViewSortDirection = "DESC";
                        break;

                    case "DESC":
                        GridViewSortDirection = "ASC";
                        break;
                }

                return GridViewSortDirection;
            }

It works perfectly.I hope it will be helpful if anyone need.Thank you

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.