解析GridView自带分页及与DropDownList结合使用
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class gridview_zidaifenye : System.Web.UI.Page
{
DBAccess db = new DBAccess();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// gvProduct.DataSource = getData();
// gvProduct.DataBind();
BindGrid();
}
}
public void BindGrid()
{
SqlCommand comm = db.CreateCommand("select * from product p,Uuser u where p.userid=u.id");
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = comm;
DataSet ds = new DataSet();
sda.Fill(ds,"Datatable");
DataView dv = ds.Tables[0].DefaultView;
GridView1.DataSource = dv;
GridView1.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.PageSize = int.Parse(DropDownList1.SelectedValue);
GridView1.PageIndex = 0;
BindGrid(); //GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
lblMsg.Text = "当前页为第" + (GridView1.PageIndex + 1).ToString() + "页,共" + (GridView1.PageCount).ToString() + "页";
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = ((GridViewPageEventArgs)e).NewPageIndex;
BindGrid(); //重新绑定GridView数据的函数
}
}