如何在GridView中实现双向排序

 

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  GridViewCheckBox : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            ViewState[
"SortOrder"= "firstname";
            ViewState[
"OrderDire"= "asc";
            GridViewDataBind();
        }

            
    }


    
private void GridViewDataBind()
    
{
        
string connStr = ConfigurationManager.ConnectionStrings["northwind"].ConnectionString;
        SqlConnection conn 
= new SqlConnection(connStr);
        SqlDataAdapter da 
= new SqlDataAdapter("select firstname, lastname, city, birthdate from employees", conn);
        conn.Open();
        DataSet ds 
= new DataSet();
        
try
        
{
            da.Fill(ds, 
"testTable");
            DataView view 
= ds.Tables["testTable"].DefaultView;
            
string sort = (string)ViewState["SortOrder"+ " " + (string)ViewState["OrderDire"];
            view.Sort 
= sort;
            
//dgCheckBox.DataSource = ds.Tables["testTable"].DefaultView;
            dgCheckBox.DataSource = view;
            dgCheckBox.DataBind();
        }

        
catch (Exception error)
        
{
            Response.Write(error.ToString());
        }

        
finally 
        
{
            conn.Close();
        }

    }

    
protected void cmdSelectAll_Click(object sender, EventArgs e)
    
{
        System.Web.UI.WebControls.CheckBox chkEport;
        
if (cmdSelectAll.Text == "全选")
        
{
            
foreach (GridViewRow gridviewrow in dgCheckBox.Rows)
            
{
                chkEport 
= (CheckBox)gridviewrow.FindControl("chkEport");
                chkEport.Checked 
= true;
            }

            cmdSelectAll.Text 
= "取消";
        }

        
else
        
{
            
foreach (GridViewRow gridviewrow in dgCheckBox.Rows)
            
{
                chkEport 
= (CheckBox)gridviewrow.FindControl("chkEport");
                chkEport.Checked 
= false;
            }

            cmdSelectAll.Text 
= "全选";
        }

    }

    
protected void cmdFindSelected_Click(object sender, EventArgs e)
    
{
        System.Web.UI.WebControls.CheckBox chkEport;
        
string strID;
        System.Text.StringBuilder strMsg 
= new System.Text.StringBuilder("选中项的City字段值分别为:<hr color=red>");
        
foreach (GridViewRow gridviewrow in dgCheckBox.Rows)
        
{
            chkEport 
= (CheckBox)gridviewrow.FindControl("chkEport");
            
if (chkEport.Checked)
            
{
                strID 
= ((Label)(gridviewrow.FindControl("lblCity"))).Text;
                strMsg.Append(strID 
+ "<br><hr color=red>");
            }

            message.Text 
= strMsg.ToString();
        }

    }


    
protected void dgCheckBox_Sorting(object sender, GridViewSortEventArgs e)
    
{
        
string vortExp = e.SortExpression;
        
if (ViewState["SortOrder"].ToString() == vortExp)
        
{
            
if ((string)ViewState["OrderDire"== "desc")
            
{
                ViewState[
"OrderDire"= "asc";
            }

            
else
            
{
                ViewState[
"OrderDire"= "asc";
            }

        }

        
else
        
{
            ViewState[
"SortOrder"= e.SortExpression;
        }

        GridViewDataBind();
    }

    
protected void dgCheckBox_PageIndexChanging(object sender, GridViewPageEventArgs e)
    
{
        dgCheckBox.PageIndex 
= e.NewPageIndex;
        GridViewDataBind();
    }

}

GridViewCheckBox.aspx代码如下:

     < form id = " form1 "  runat = " server " >
    
< div >
        
< table style = " width: 566px " >
            
< tr >
                
< td style = " width: 112px "   >
                    
< asp:GridView ID = " dgCheckBox "  runat = " server "  GridLines = " vertical "  AutoGenerateColumns = " false "  PagerSettings - Mode = " numeric "  Width = " 446px "
                     AllowSorting
= " true "  AllowPaging = " true "  PageSize = " 5 "    OnSorting = " dgCheckBox_Sorting "  OnPageIndexChanging = " dgCheckBox_PageIndexChanging " >
                    
< SelectedRowStyle Font - Bold = " True "  ForeColor = " White "  BackColor = " #008A8C " />
                    
< AlternatingRowStyle BackColor = " AppWorkspace " />
                    
< RowStyle ForeColor = " ActiveBorder "  BackColor = " ActiveCaption "   />
                    
< HeaderStyle Font - Bold = " True "  ForeColor = " ActiveCaptionText "  BackColor = " AliceBlue "   />
                    
< FooterStyle ForeColor = " AppWorkspace "  BackColor = " Aqua "   />
                    
< Columns >
                    
< asp:TemplateField >
                    
< ItemTemplate >
                    
< asp:CheckBox ID = " chkEport "  runat = " server "   />
                    
</ ItemTemplate >
                    
< EditItemTemplate >
                    
< asp:CheckBox ID = " chkEportON "  runat = " server "   />
                    
</ EditItemTemplate >
                    
</ asp:TemplateField >
                    
< asp:BoundField DataField = " lastname "  HeaderText = " lastname "  SortExpression = " lastname " />
                    
< asp:BoundField DataField = " firstname "  HeaderText = " firstname "  SortExpression = " firstname " />
                    
< asp:BoundField DataField = " BirthDate "  HeaderText = " BirthDate "  SortExpression = " BirthDate "  DataFormatString = " {0:D} "   />
                    
< asp:TemplateField >
                    
< ItemTemplate >
                    
< asp:Label ID = " lblCity "  runat = " server "  Text = ' <%#Eval("city") %> ' ></ asp:Label >
                    
</ ItemTemplate >
                    
</ asp:TemplateField >
                    
                    
</ Columns >
                    
< PagerStyle HorizontalAlign = " Right "  ForeColor = " Aquamarine "  BackColor = " Azure " />
                    
</ asp:GridView >
                
</ td >
            
</ tr >
            
< tr >
            
< td style = " width: 112px " >
            
< asp:Button ID = " cmdSelectAll "  runat = " server "  Text = " 全选 "  OnClick = " cmdSelectAll_Click "   />& nbsp; < asp:Button ID = " cmdFindSelected "  runat = " server "  Text = " 取得选择的项 "  OnClick = " cmdFindSelected_Click "   />
            
</ td >
            
</ tr >< tr >
            
< td >< asp:Label ID = " message "  runat = " server " ></ asp:Label ></ td >
            
</ tr >
        
</ table >
    
    
</ div >
    
</ form >

 GridViewCheckBox.aspx.cs代码如下:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值