วิธีจัดเรียงคอลัมน์ GridView ใน ASP.net Web Application

ฉันได้สร้างเว็บฟอร์มโดยใช้มาสเตอร์เพจในเว็บแอปพลิเคชันของฉัน และฉันได้สร้าง GridView และฉันต้องการเรียงลำดับคอลัมน์ การออกแบบ gridview ของฉันดังนี้:

<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>

person Ram    schedule 09.12.2016    source แหล่งที่มา
comment
สิ่งนี้ช่วยคุณได้หรือไม่? stackoverflow.com/a/10498012/1166719   -  person Pierre-Loup Pagniez    schedule 09.12.2016
comment
เพียงเปิดใช้งานการเรียงลำดับบน gridview   -  person fnostro    schedule 09.12.2016
comment
ฉันทำสิ่งนั้นไปแล้วalloworting=true; แต่มันแสดงข้อความแสดงข้อผิดพลาด   -  person Ram    schedule 09.12.2016
comment
ฉันทำอย่างนั้น AllowSorting=True แล้ว แต่มันแสดงข้อความแสดงข้อผิดพลาด เหตุการณ์ที่เรียกใช้ GridView 'GridView1' การเรียงลำดับซึ่งไม่ได้รับการจัดการ ฉันกำหนด SortExpression=sno และอื่นๆ   -  person Ram    schedule 09.12.2016
comment
ขอบคุณสำหรับคำตอบของคุณ Pierre-Loup Pagniez และ fnostro กระทู้ที่คุณแนะนำใช้งานได้สำหรับฉัน ฉันต้องการทำสิ่งนี้โดยไม่ต้อง postback ด้วยความช่วยเหลือของ javascript และ jquery   -  person Ram    schedule 09.12.2016
comment
การเรียงลำดับ Gridview โดยใช้คุณสมบัติการเรียงลำดับในตัวนั้นจำเป็นต้องมี postback ทุกครั้งที่คลิกส่วนหัวของคอลัมน์ ดังที่กล่าวไปแล้ว Gridview เรนเดอร์ตารางและมีโค้ดบุคคลที่สาม jquery/js ที่สามารถจัดการฝั่งไคลเอ็นต์การเรียงลำดับด้วยการเพิ่ม html และหรือแอตทริบิวต์ที่เหมาะสม แต่ฉันอยากจะแนะนำต่อต้านมัน   -  person fnostro    schedule 10.12.2016
comment
ฉันได้เรียงลำดับคอลัมน์ใน Gridview ดังที่แสดงด้านล่าง ตอนนี้ฉันใช้ UpdatePanel สำหรับ Gridview แต่มันทริกเกอร์ postback ขณะเรียงลำดับ ฉันจะเรียงลำดับใน postback บางส่วนได้อย่างไร@fnostro   -  person Ram    schedule 20.12.2016


คำตอบ (1)


ฉันเรียงลำดับคอลัมน์ใน Gridview ด้วยความช่วยเหลือของเธรดที่มีอยู่ ที่นี่

การเข้ารหัสโครงการของฉันเป็นดังนี้

<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>

รหัสด้านหลังคือ

 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();

        }

การเข้ารหัสสำหรับการเรียงลำดับ GridView โดยใช้ฟังก์ชันการเรียงลำดับ ASP.net Buildin

 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;
            }

มันทำงานได้อย่างสมบูรณ์แบบ ฉันหวังว่ามันจะเป็นประโยชน์หากใครต้องการ ขอบคุณ

person Ram    schedule 16.12.2016