.net中数据源配置数据库的内容使其显示在gridview里面;数据库有10条记录,怎样设置数据源使其只显示5项
3个回答
展开全部
用gridview控件来显示数据
(设置gridview的关键字:datakeynames:书号,并在数据库中把书号设为主键)
分页显示:allowpaging: true (允许分页显示) ; pagesize: 5 (每页5个记录)
protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack)
dispbook();
}
protected void dispbook()
{ string strconn = "server=(local);database=dangdang;uid=sa;pwd=";
SqlConnection sqlconn = new SqlConnection(strconn);
sqlconn.Open();
string strsel = "select * from book"; //"select 书号,作者,出版社,单价 from book";
SqlDataAdapter ada = new SqlDataAdapter(strsel, sqlconn);
DataSet ds = new DataSet();
ada.Fill(ds, "book");
GridView1 .DataSource =ds.Tables ["book"].DefaultView ;
GridView1.DataBind();
sqlconn.Close();
}
在事件PageIndexChanging中双击进去写代码:在点击第二页时可以显示第二页的代码咯
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{ GridView1.PageIndex = e.NewPageIndex;
dispbook();
}
(设置gridview的关键字:datakeynames:书号,并在数据库中把书号设为主键)
分页显示:allowpaging: true (允许分页显示) ; pagesize: 5 (每页5个记录)
protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack)
dispbook();
}
protected void dispbook()
{ string strconn = "server=(local);database=dangdang;uid=sa;pwd=";
SqlConnection sqlconn = new SqlConnection(strconn);
sqlconn.Open();
string strsel = "select * from book"; //"select 书号,作者,出版社,单价 from book";
SqlDataAdapter ada = new SqlDataAdapter(strsel, sqlconn);
DataSet ds = new DataSet();
ada.Fill(ds, "book");
GridView1 .DataSource =ds.Tables ["book"].DefaultView ;
GridView1.DataBind();
sqlconn.Close();
}
在事件PageIndexChanging中双击进去写代码:在点击第二页时可以显示第二页的代码咯
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{ GridView1.PageIndex = e.NewPageIndex;
dispbook();
}
追问
那如果是用datalist显示的数据集呢?没有allowsize属性,那怎么处理呢?先为前面的回答谢谢!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询