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控件内加入如下代码:
<columns>
<asp教程:boundfield headertext="学号" datafield="xuehao" /> //绑定数据源的xuehao列
<asp:boundfield headertext="姓名" datafield="name" /> //绑定数据源的name列
<asp:boundfield headertext="密码" datafield="pasword" /> //绑定数据源的pasword列
<asp:templatefield headertext="删除">
<itemtemplate>
<asp:checkbox id="checkbox1" runat="server" />
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="编辑">
<itemtemplate>
<asp:linkbutton id="linkbutton1" runat="server" causesvalidation="false" commandname="zuoza" commandargument="<%# gridviewx.rows.count %>" text="编辑"></asp:linkbutton>
</itemtemplate>
</asp:templatefield>
</columns>
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 + "<br>"); //输出当前列的主键值
}
else
{
response.write("shy" + "</br>");
}
}
}
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、在<<asp:gridview id=......>后添加,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按钮,分别设置如下:
<asp:linkbutton id="lnkbtnfrist" runat="server" onclick="lnkbtnfrist_click">首页</asp:linkbutton>
<asp:linkbutton id="lnkbtnpre" runat="server" onclick="lnkbtnpre_click">上一页</asp:linkbutton>
<asp:linkbutton id="lnkbtnnext" runat="server" onclick="lnkbtnnext_click">下一页</asp:linkbutton>
<asp:linkbutton id="lnkbtnlast" runat="server" onclick="lnkbtnlast_click">尾页</asp: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();
}