分页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
DataBind();
}
排序
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ViewState["sortColumn"] = " ";
ViewState["sortDirection"] = " ";
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
GridView1.EditIndex = -1;
if (ViewState["sortColumn"].ToString() == e.SortExpression.ToString())
{
if ("ASC" == ViewState["sortDirection"].ToString())
{
ViewState["sortDirection"] = "DESC";
}
else
{
ViewState["sortDirection"] = "ASC";
}
}
else
{
ViewState["sortColumn"] = e.SortExpression.ToString();
ViewState["sortDirection"] = "ASC";
}
排序 (VB.NET)
Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
Dim strFiled As String = e.SortExpression.Trim.ToString
If ViewState("SortDirction") = "" Then
ViewState("SortDirction") = "desc" '这个与填充gridview语句的排序相反
Else
If ViewState("SortDirction").ToString = "asc" Then
ViewState("SortDirction") = "desc"
Else
ViewState("SortDirction") = "asc"
End If
End If
Dim sql As String = "select * from tb_pic order by " + strFiled + " " + ViewState("SortDirction").ToString
Dim cn As SqlConnection = New SqlConnection("data source=(local);initial catalog=wddlj;user id=sa;password=20096327")
Dim ds As Data.DataSet = New Data.DataSet
Dim ad As SqlDataAdapter = New SqlDataAdapter(sql, cn)
ad.Fill(ds, "sc_news")
GridView1.DataSource = ds
GridView1.DataBind()
End Sub