gridview分页与详细使用方法
1. 把gridview控件拖放到界面上
2. 用代码设置数据源绑定gridview控件
oledbcommand command = new oledbcommand();
string sql = "select * from admin"; //绑定到数据表admin
command.commandtext = sql;
command.connection = shyconnection.conn;
shyconnection.conn.open(); //打开数据连接
oledbdataadapter adapter = new oledbdataadapter(command);
dataset ds = new dataset();
adapter.fill(ds);
gridviewx.datasource = ds;
gridviewx.datakeynames = new string[] { "id" }; 控件的主键数组
gridviewx.databind();
shyconnection.conn.close(); //关闭数据连接
3. 在源界面gridview控件内加入如下代码:
//绑定数据源的xuehao列 //绑定数据源的name列
//绑定数据源的pasword列
" text="编辑">
4.判断gridview控件里面每列的checkbox控件是否被中
protected void button1_click(object sender, eventargs e)
{
for (int i = 0; i <= gridviewx.rows.count - 1; i++)
{
checkbox cbox = (checkbox)gridviewx.rows[i].findcontrol("checkbox1"); //在每列中查找名称是checkbox1的checkbox控件,然后强制实例转换为checkbox的对象
if (cbox.checked == true)
{
response.write((int)gridviewx.datakeys[i].value + "
"); //输出当前列的主键值}
else
{
response.write("shy" + "");
}
}
}
5.如何点击编辑的时候触发函数gridviewx_rowcommand:
protected void gridviewx_rowcommand(object sender, gridviewcommandeventargs e)
{
if (e.commandname == "edit")
{
string id = "";
int index = convert.toint32((string)e.commandargument);
id = convert.tostring(gridviewx.datakeys[index].value.tostring());
//现在获取了所选记录的主键id
}
}
6.当控件加载的时候触发这个事件函数:gridviewx_rowdatabound
protected void gridviewx_rowdatabound(object sender, gridviewroweventargs e)
{
this.lblcurrentpage.text = string.format("当前第{0}页/总共{1}页", this.gridviewx.pageindex + 1, this.gridviewx.pagecount);
//遍历所有行设置边框样式
foreach (tablecell tc in e.row.cells)
{
tc.attributes["style"] = "border-color:black";
}
//用索引来取得编号
if (e.row.rowindex != -1)
{
int id = gridviewx.pageindex * gridviewx.pagesize + e.row.rowindex + 1;
//e.row.cells[0].text = id.tostring();
}
}
分页代码
grdview分页的功能。
操作如下:
1、更改grdview控件的allowpaging属性为true。
2、更改grdview控件的pagesize属性为 任意数值(默认为10)
3、更改grdview控件的pagesetting->mode为numeric等(默认为numeric)该属性为分页样式。
gridview属性设置好了,从页面上也能看到分页样式。现在开始实现分页的功能:
1、在<后添加,onpageindexchanging="gridview1_pageindexchanging"
2、在对应的aspx.cs中添加:
protected void gridview1_pageindexchanging(object sender, gridviewpageeventargs e)
{
gridview1.pageindex = e.newpageindex;
initpage(); //重新绑定gridview数据的函数
}
3、
gridview1.pageindex = e.newpageindex;
页制作:
1. 先把gridview(gridviewx)控件拖放到界面上,绑定数据(bind());同时对dropdownlist做如下操作:this.ddlcurrentpage.items.clear();
for (int i = 1; i <= this.gridviewx.pagecount; i++)
{
this.ddlcurrentpage.items.add(i.tostring());
}
1. this.ddlcurrentpage.selectedindex = this.gridviewx.pageindex;
2. 设置gridview的属性allowpaging="true" pagesize="n"
3. 拖放几个linkbutton按钮,分别设置如下:
首页
上一页
下一页
尾页
4. 设置几个文本按钮的事件处理函数:
protected void lnkbtnpre_click(object sender, eventargs e)
{
if (this.gridviewx.pageindex > 0)
{
this.gridviewx.pageindex = this.gridviewx.pageindex - 1;
bind();
}
}
protected void lnkbtnnext_click(object sender, eventargs e)
{
if (this.gridviewx.pageindex < this.gridviewx.pagecount)
{
this.gridviewx.pageindex = this.gridviewx.pageindex + 1;
bind();
}
}
protected void lnkbtnlast_click(object sender, eventargs e)
{
this.gridviewx.pageindex = this.gridviewx.pagecount;
bind();
}
5.dropdownlist的事件处理函数:
protected void dropdownlist1_selectedindexchanged(object sender, eventargs e)
{
this.gridviewx.pageindex = this.ddlcurrentpage.selectedindex;
bind();
}