Asp. Two-way sort of gridview in the net

Source: Internet
Author: User
Tags object bind net sort tostring
Effect Picture: Point Name each 2 times of the order, point other also can.

Background code:

Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Data.SqlClient;
public partial class Default3:System.Web.UI.Page
{

Qingqing Moon's Blog http://blog.csdn.net/21aspnet
SqlConnection Sqlcon;
String Strcon = "Data source= (local);D atabase= North Wind trade; Uid=sa; Pwd= ";
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
viewstate["SortOrder"] = "identity card number";
viewstate["Orderdire"] = "ASC";
Bind ();
}
}
protected void Gridview1_sorting (object sender, GridViewSortEventArgs e)
{
string spage = E.sortexpression;
if (viewstate["SortOrder"]. ToString () = = spage)
{
if (viewstate["Orderdire"]. ToString () = = "Desc")
viewstate["Orderdire"] = "ASC";
Else
viewstate["Orderdire"] = "Desc";
}
Else
{
viewstate["SortOrder"] = e.sortexpression;
}
Bind ();
}

public void bind ()
{
       
String sqlstr = "SELECT Top 5 * from flying Fox Studio";
Sqlcon = new SqlConnection (Strcon);
SqlDataAdapter Myda = new SqlDataAdapter (Sqlstr, Sqlcon);
DataSet myds = new DataSet ();
Sqlcon. Open ();
Myda. Fill (myDS, "Flying Fox Studio");
DataView view = myDS. Tables["Flying Fox Studio"]. DefaultView;
String sort = (string) viewstate["SortOrder"] + "" + (String) viewstate["Orderdire"];
View. sort = sort;
Gridview1.datasource = view;
Gridview1.databind ();
Sqlcon. Close ();
}
}

前台主要代码:

<asp:gridview id= "GridView1" runat= "Server" allowsorting= "True" autogeneratecolumns= "False"
cellpadding= "3" font-size= "9pt" onsorting= "gridview1_sorting" back Color= "White" bordercolor= "#CCCCCC" borderstyle= "None" borderwidth= "1px"
<footerstyle B Ackcolor= "White" forecolor= "#000066"/>
<columns>
&L T;asp:boundfield datafield= "ID number" headertext= "User ID" sortexpression= "identity card number"/>
<asp: BoundField datafield= "name headertext=" user name

" SortExpression="姓名"/>
                            <asp:BoundField DataField="员工性别" HeaderText="性别" SortExpression="员工性别"/>
                            <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址"/>
                               
                        </Columns>
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>

Author of this article: unknown



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.