在GridView中实现双向排序的步骤
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.Data.SqlClient;
publicpartialclassGridViewCheckBox:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!IsPostBack)
{
ViewState["SortOrder"]="firstname";
ViewState["OrderDire"]="asc";
GridViewDataBind();
}
}
privatevoidGridViewDataBind()
{
stringconnStr=ConfigurationManager.ConnectionStrings["northwind"].ConnectionString;
SqlConnectionconn=newSqlConnection(connStr);
SqlDataAdapterda=newSqlDataAdapter("selectfirstname,lastname,city,birthdatefromemployees",conn);
conn.Open();
DataSetds=newDataSet();
try
{
da.Fill(ds,"testTable");
DataViewview=ds.Tables["testTable"].DefaultView;
stringsort=(string)ViewState["SortOrder"]+""+(string)ViewState["OrderDire"];
view.Sort=sort;
//dgCheckBox.DataSource=ds.Tables["testTable"].DefaultView;
dgCheckBox.DataSource=view;
dgCheckBox.DataBind();
}
catch(Exceptionerror)
{
Response.Write(error.ToString());
}
finally
{
conn.Close();
}
}
protectedvoidcmdSelectAll_Click(objectsender,EventArgse)
{
System.Web.UI.WebControls.CheckBoxchkEport;
if(cmdSelectAll.Text=="全选")
{
foreach(GridViewRowgridviewrowindgCheckBox.Rows)
{
chkEport=(CheckBox)gridviewrow.FindControl("chkEport");
chkEport.Checked=true;
}
cmdSelectAll.Text="取消";
}
else
{
foreach(GridViewRowgridviewrowindgCheckBox.Rows)
{
chkEport=(CheckBox)gridviewrow.FindControl("chkEport");
chkEport.Checked=false;
}
cmdSelectAll.Text="全选";
}
}
protectedvoidcmdFindSelected_Click(objectsender,EventArgse)
{
System.Web.UI.WebControls.CheckBoxchkEport;
stringstrID;
System.Text.StringBuilderstrMsg=newSystem.Text.StringBuilder("选中项的City字段值分别为:<hrcolor=red>");
foreach(GridViewRowgridviewrowindgCheckBox.Rows)
{
chkEport=(CheckBox)gridviewrow.FindControl("chkEport");
if(chkEport.Checked)
{
strID=((Label)(gridviewrow.FindControl("lblCity"))).Text;
strMsg.Append(strID+"<br><hrcolor=red>");
}
message.Text=strMsg.ToString();
}
}
protectedvoiddgCheckBox_Sorting(objectsender,GridViewSortEventArgse)
{
stringvortExp=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();
}
protectedvoiddgCheckBox_PageIndexChanging(objectsender,GridViewPageEventArgse)
{
dgCheckBox.PageIndex=e.NewPageIndex;
GridViewDataBind();
}
}
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.Data.SqlClient;
publicpartialclassGridViewCheckBox:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!IsPostBack)
{
ViewState["SortOrder"]="firstname";
ViewState["OrderDire"]="asc";
GridViewDataBind();
}
}
privatevoidGridViewDataBind()
{
stringconnStr=ConfigurationManager.ConnectionStrings["northwind"].ConnectionString;
SqlConnectionconn=newSqlConnection(connStr);
SqlDataAdapterda=newSqlDataAdapter("selectfirstname,lastname,city,birthdatefromemployees",conn);
conn.Open();
DataSetds=newDataSet();
try
{
da.Fill(ds,"testTable");
DataViewview=ds.Tables["testTable"].DefaultView;
stringsort=(string)ViewState["SortOrder"]+""+(string)ViewState["OrderDire"];
view.Sort=sort;
//dgCheckBox.DataSource=ds.Tables["testTable"].DefaultView;
dgCheckBox.DataSource=view;
dgCheckBox.DataBind();
}
catch(Exceptionerror)
{
Response.Write(error.ToString());
}
finally
{
conn.Close();
}
}
protectedvoidcmdSelectAll_Click(objectsender,EventArgse)
{
System.Web.UI.WebControls.CheckBoxchkEport;
if(cmdSelectAll.Text=="全选")
{
foreach(GridViewRowgridviewrowindgCheckBox.Rows)
{
chkEport=(CheckBox)gridviewrow.FindControl("chkEport");
chkEport.Checked=true;
}
cmdSelectAll.Text="取消";
}
else
{
foreach(GridViewRowgridviewrowindgCheckBox.Rows)
{
chkEport=(CheckBox)gridviewrow.FindControl("chkEport");
chkEport.Checked=false;
}
cmdSelectAll.Text="全选";
}
}
protectedvoidcmdFindSelected_Click(objectsender,EventArgse)
{
System.Web.UI.WebControls.CheckBoxchkEport;
stringstrID;
System.Text.StringBuilderstrMsg=newSystem.Text.StringBuilder("选中项的City字段值分别为:<hrcolor=red>");
foreach(GridViewRowgridviewrowindgCheckBox.Rows)
{
chkEport=(CheckBox)gridviewrow.FindControl("chkEport");
if(chkEport.Checked)
{
strID=((Label)(gridviewrow.FindControl("lblCity"))).Text;
strMsg.Append(strID+"<br><hrcolor=red>");
}
message.Text=strMsg.ToString();
}
}
protectedvoiddgCheckBox_Sorting(objectsender,GridViewSortEventArgse)
{
stringvortExp=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();
}
protectedvoiddgCheckBox_PageIndexChanging(objectsender,GridViewPageEventArgse)
{
dgCheckBox.PageIndex=e.NewPageIndex;
GridViewDataBind();
}
}
本文地址:http://www.45fan.com/dnjc/67386.html