GridView常用操作事件图文介绍
程序员文章站
2024-03-06 14:03:38
对于gridview学net的同学再熟悉不过,但是其中功能事件是否能编码熟练实现?前不久看点博文,以及资料,综合自己的一些想法,汇总如下:
数据库设计如下,以便更好理...
对于gridview学net的同学再熟悉不过,但是其中功能事件是否能编码熟练实现?前不久看点博文,以及资料,综合自己的一些想法,汇总如下:
数据库设计如下,以便更好理解:
设计:
实现:
gridview无代码分页排序
小实例:
allowsorting设为true,aspx代码中是allowsorting="true";
运行结果:
gridview选中,编辑,取消,删除
小实例:
gridview.aspx
复制代码 代码如下:
<asp:gridview id="gridview1" runat="server" height="138px" width="1117px"
allowpaging="true" allowsorting="true" autogeneratecolumns="false"
onpageindexchanging="gridview1_pageindexchanging"
onrowcancelingedit="gridview1_rowcancelingedit"
onrowdeleting="gridview1_rowdeleting" onrowediting="gridview1_rowediting"
onrowupdating="gridview1_rowupdating">
<columns>
<asp:templatefield headertext="身份证号" sortexpression="pid">
<itemtemplate>
<asp:label id="label1" runat="server" text='<%# bind("pid") %>'></asp:label>
</itemtemplate>
</asp:templatefield>
<%-- <asp:boundfield datafield="pid" headertext="身份证号" sortexpression="pid" />--%>
<asp:boundfield datafield="pname" headertext="姓名" sortexpression="pname" />
<asp:boundfield datafield="psex" headertext="性别" sortexpression="psex" />
<asp:boundfield datafield="padress" headertext="地址" sortexpression="padress" />
<asp:boundfield datafield="pyoubiao" headertext="邮编" sortexpression="pyoubiao" />
<asp:boundfield datafield="pprice" headertext="工资起价" sortexpression="pprice" />
<asp:commandfield headertext="选择" showselectbutton="true" />
<asp:commandfield headertext="编辑" showeditbutton="true" />
<asp:commandfield headertext="删除" showdeletebutton="true" />
</columns>
<emptydatarowstyle backcolor="red" />
<headerstyle backcolor="#0000cc" font-bold="true" forecolor="white" />
<pagerstyle backcolor="#864" horizontalalign="center" forecolor="white"/>
</asp:gridview>
gridview.aspx.cs
复制代码 代码如下:
//公有数据
sqlconnection con = new sqlconnection(configurationmanager.connectionstrings["sqlcon"].connectionstring);
sqlcommand cmd;
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
bind(); //正常绑定
}
}
/// <summary>
/// 主题:绑定数据,查询显示个人信息
/// 时间:2012年11月21日14:54:13
/// 描述:通过简单的方法绑定,实现数据显示
/// 作者:小伙
/// </summary>
public void bind()
{
//数据连接web.config中已配置的数据
sqldataadapter da = new sqldataadapter("select top 5 * from person", con);//使用数据适配器内置查询,自动开闭数据库
dataset ds = new dataset();//使用数据集,采取断开式访问数据
da.fill(ds, "person");
gridview1.datasource = ds;
gridview1.datakeynames=new string[]{"pid"};//设置主键
gridview1.databind();
}
//实现分页
protected void gridview1_pageindexchanging(object sender, gridviewpageeventargs e)
{
gridview1.pageindex = e.newpageindex;
this.bind();
}
//删除
protected void gridview1_rowdeleting(object sender, gridviewdeleteeventargs e)
{
string sqlstr = "delete from person where pid='" + gridview1.datakeys[e.rowindex].value.tostring() + "'";
con.open();
cmd = new sqlcommand(sqlstr, con);
cmd.executenonquery();
con.close();
bind();
}
//取消编辑
protected void gridview1_rowcancelingedit(object sender, gridviewcancelediteventargs e)
{
gridview1.editindex = -1;
bind();
}
//编辑
protected void gridview1_rowediting(object sender, gridviewediteventargs e)
{
gridview1.editindex = e.neweditindex;
bind();
}
//更新
protected void gridview1_rowupdating(object sender, gridviewupdateeventargs e)
{
string sqlstr = "update person set pname='"
+((textbox)(gridview1.rows[e.rowindex].cells[1].controls[0])).text.tostring().trim() + "',psex='"
+((textbox)(gridview1.rows[e.rowindex].cells[2].controls[0])).text.tostring().trim()+"',padress='"
+((textbox)(gridview1.rows[e.rowindex].cells[3].controls[0])).text.tostring().trim()+"',pyoubiao='"
+((textbox)(gridview1.rows[e.rowindex].cells[4].controls[0])).text.tostring().trim()+"',pprice='"
+((textbox)(gridview1.rows[e.rowindex].cells[5].controls[0])).text.tostring().trim()+"' where pid='"
+gridview1.datakeys[e.rowindex].value.tostring()+"'";
cmd = new sqlcommand(sqlstr, con);
con.open();
cmd.executenonquery();
con.close();
gridview1.editindex = -1;
bind();
}
运行结果:
运行初始页:
点击编辑:
更新后:
gridview正反双向排序
小实例:
gridview.aspx
复制代码 代码如下:
<asp:gridview id="gridview1" runat="server" height="138px" width="1117px"
allowpaging="true" allowsorting="true" autogeneratecolumns="false"
onpageindexchanging="gridview1_pageindexchanging"
onrowcancelingedit="gridview1_rowcancelingedit"
onrowdeleting="gridview1_rowdeleting" onrowediting="gridview1_rowediting"
onrowupdating="gridview1_rowupdating" onsorting="gridview1_sorting">
<columns>
<%--<asp:templatefield headertext="身份证号" sortexpression="pid">
<itemtemplate>
<asp:label id="label1" runat="server" text='<%# bind("pid") %>'></asp:label>
</itemtemplate>
</asp:templatefield>--%>
<asp:boundfield datafield="pid" headertext="身份证号" sortexpression="pid" />
<asp:boundfield datafield="pname" headertext="姓名" sortexpression="pname" />
<asp:boundfield datafield="psex" headertext="性别" sortexpression="psex" />
<asp:boundfield datafield="padress" headertext="地址" sortexpression="padress" />
<asp:boundfield datafield="pyoubiao" headertext="邮编" sortexpression="pyoubiao" />
<asp:boundfield datafield="pprice" headertext="工资起价" sortexpression="pprice" />
<asp:commandfield headertext="选择" showselectbutton="true" />
<asp:commandfield headertext="编辑" showeditbutton="true" />
<asp:commandfield headertext="删除" showdeletebutton="true" />
</columns>
<emptydatarowstyle backcolor="red" />
<headerstyle backcolor="#0000cc" font-bold="true" forecolor="white" />
<pagerstyle backcolor="#864" horizontalalign="center" forecolor="white"/>
</asp:gridview>
gridview.aspx.cs
复制代码 代码如下:
//公有数据
sqlconnection con = new sqlconnection(configurationmanager.connectionstrings["sqlcon"].connectionstring);
sqlcommand cmd;
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
viewstate["sortorder"] = "pname";
viewstate["orderdire"] = "asc";
sortbind1();//排序绑定
}
}
//排序绑定
public void sortbind1()
{
string sqlstr = "select top 5 * from person";
sqldataadapter myda = new sqldataadapter(sqlstr, con);
dataset ds = new dataset();
con.open();
myda.fill(ds, "person");
dataview view = ds.tables["person"].defaultview;
string sort = (string)viewstate["sortorder"] + " " + (string)viewstate["orderdire"];
view.sort = sort;
gridview1.datasource = view;
gridview1.databind();
con.close();
}
//排序
protected void gridview1_sorting(object sender, gridviewsorteventargs e)
{
string spage = e.sortexpression;
if (viewstate["sortorder"].tostring() == spage)
{
if (viewstate["orderdire"].tostring() == "desc")
{
viewstate["orderdire"] = "asc";
}
else
{
viewstate["orderdire"] = "desc";
}
}
else
{
viewstate["sortorder"] = e.sortexpression;
}
sortbind1();
}
运行结果:
排序前:
排序后:
gridview和下拉菜单dropdownlist结合
小实例:
gridview.aspx
复制代码 代码如下:
<asp:gridview id="gridview1" runat="server" height="138px" width="1117px"
allowpaging="true" allowsorting="true" autogeneratecolumns="false"
onpageindexchanging="gridview1_pageindexchanging"
onrowcancelingedit="gridview1_rowcancelingedit"
onrowdeleting="gridview1_rowdeleting" onrowediting="gridview1_rowediting"
onrowupdating="gridview1_rowupdating" onsorting="gridview1_sorting">
<columns>
<%--<asp:templatefield headertext="身份证号" sortexpression="pid">
<itemtemplate>
<asp:label id="label1" runat="server" text='<%# bind("pid") %>'></asp:label>
</itemtemplate>
</asp:templatefield>--%>
<asp:boundfield datafield="pid" headertext="身份证号" sortexpression="pid" />
<asp:boundfield datafield="pname" headertext="姓名" sortexpression="pname" />
<asp:templatefield headertext="性别" sortexpression="psex" >
<itemtemplate>
<asp:dropdownlist id="dropdownlist1" runat="server" datasource='<%# ddlbind()%>' datavaluefield="psex" datatextfield="psex"/>
</itemtemplate>
</asp:templatefield>
<asp:boundfield datafield="padress" headertext="地址" sortexpression="padress" />
<asp:boundfield datafield="pyoubiao" headertext="邮编"
sortexpression="pyoubiao" />
<asp:boundfield datafield="pprice" headertext="工资起价" sortexpression="pprice" />
<asp:commandfield headertext="选择" showselectbutton="true" />
<asp:commandfield headertext="编辑" showeditbutton="true" />
<asp:commandfield headertext="删除" showdeletebutton="true" />
</columns>
<emptydatarowstyle backcolor="red" />
<headerstyle backcolor="#0000cc" font-bold="true" forecolor="white" />
<pagerstyle backcolor="#864" horizontalalign="center" forecolor="white"/>
</asp:gridview>
gridview.aspx.cs
复制代码 代码如下:
//公有数据
sqlconnection con = new sqlconnection(configurationmanager.connectionstrings["sqlcon"].connectionstring);
sqlcommand cmd;
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
drpbind();//dropdown绑定
}
}
//dropdownlist绑定数据
dropdownlist ddl;
public void drpbind()
{
string sqlstr = "select top 5 * from person";
con.open();
sqldataadapter myda = new sqldataadapter(sqlstr, con);
dataset ds = new dataset();
myda.fill(ds, "person");
gridview1.datasource = ds;
gridview1.databind();
for (int i = 0; i <= gridview1.rows.count - 1; i++)
{
datarowview mydrv = ds.tables["person"].defaultview[i];
if (convert.tostring(mydrv["psex"]).trim() == "男")
{
ddl = (dropdownlist)gridview1.rows[i].findcontrol("dropdownlist1");
ddl.selectedindex = 0;
}
else if (convert.tostring(mydrv["psex"]).trim() == "女")
{
ddl = (dropdownlist)gridview1.rows[i].findcontrol("dropdownlist1");
ddl.selectedindex = 1;
}
}
con.close();
}
//gridview和下拉菜单dropdownlist结合,前台性别列datasouce调用
public sqldatareader ddlbind()
{
string sqlstr = "select distinct psex from person";//distinct只显示一次性别
using (cmd = new sqlcommand(sqlstr, con))
{
con.close();
con.open();
return cmd.executereader();
}
}
运行结果:
gridview和checkbox结合
小实例:
gridview.aspx
复制代码 代码如下:
<form id="form1" runat="server">
<asp:gridview id="gridview1" runat="server" height="138px" width="1117px"
allowpaging="true" allowsorting="true" autogeneratecolumns="false"
onpageindexchanging="gridview1_pageindexchanging"
onrowcancelingedit="gridview1_rowcancelingedit"
onrowdeleting="gridview1_rowdeleting" onrowediting="gridview1_rowediting"
onrowupdating="gridview1_rowupdating" pagesize="4">
<columns>
<asp:templatefield>
<itemtemplate>
<asp:checkbox id="checkbox1" runat="server" />
</itemtemplate>
</asp:templatefield>
<asp:boundfield datafield="pid" headertext="身份证号" sortexpression="pid" />
<asp:boundfield datafield="pname" headertext="姓名" sortexpression="pname" />
<asp:boundfield datafield="psex" headertext="性别" sortexpression="psex" />
<asp:boundfield datafield="padress" headertext="地址" sortexpression="padress" />
<asp:boundfield datafield="pyoubiao" headertext="邮编" sortexpression="pyoubiao" />
<asp:boundfield datafield="pprice" headertext="工资起价" sortexpression="pprice" />
<%-- <asp:commandfield headertext="选择" showselectbutton="true" />
<asp:commandfield headertext="编辑" showeditbutton="true" />
<asp:commandfield headertext="删除" showdeletebutton="true" />--%>
</columns>
<emptydatarowstyle backcolor="red" />
<headerstyle backcolor="#0000cc" font-bold="true" forecolor="white" />
<pagerstyle backcolor="#864" horizontalalign="center" forecolor="white"/>
</asp:gridview>
<br />
<asp:checkbox id="checkbox2" runat="server" forecolor="red" text="全选"
autopostback="true" oncheckedchanged="checkbox2_checkedchanged" />
<asp:button id="button1" runat="server" font-bold="true" forecolor="red"
text="删除" onclick="button1_click" />
<asp:button id="button2" runat="server" font-bold="true" forecolor="red"
text="取消" onclick="button2_click" />
</form>
gridview.aspx.cs
复制代码 代码如下:
//公有数据
sqlconnection con = new sqlconnection(configurationmanager.connectionstrings["sqlcon"].connectionstring);
sqlcommand cmd;
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
bind(); //正常绑定
}
}
public void bind()
{
//数据连接web.config中已配置的数据
sqldataadapter da = new sqldataadapter("select * from person", con);//使用数据适配器内置查询,自动开闭数据库
dataset ds = new dataset();//使用数据集,采取断开式访问数据
da.fill(ds, "person");
gridview1.datasource = ds;
gridview1.datakeynames=new string[]{"pid"};//设置主键
gridview1.databind();
}
//利用checkbox选中信息
protected void checkbox2_checkedchanged(object sender, eventargs e)
{
for (int i = 0; i <= gridview1.rows.count - 1; i++)
{
checkbox cbox = (checkbox)gridview1.rows[i].findcontrol("checkbox1");
if (checkbox2.checked == true)
{
cbox.checked = true;
}
else
{
cbox.checked = false;
}
}
}
//删除选中信息
protected void button1_click(object sender, eventargs e)
{
for (int i = 0; i <= gridview1.rows.count - 1; i++)
{
checkbox cbox = (checkbox)gridview1.rows[i].findcontrol("checkbox1");
if (cbox.checked == true)
{
string sqlstr = "delete from person where pid='" + gridview1.datakeys[i].value + "'";
cmd = new sqlcommand(sqlstr, con);
con.open();
cmd.executenonquery();
con.close();
}
}
bind();
}
//取消选中信息
protected void button2_click(object sender, eventargs e)
{
checkbox2.checked = false;
for (int i = 0; i <= gridview1.rows.count - 1; i++)
{
checkbox cbox = (checkbox)gridview1.rows[i].findcontrol("checkbox1");
cbox.checked = false;
}
}
鼠标移到gridview某一行时改变该行的背景色方法一
小实例:
gridview.aspx
复制代码 代码如下:
<asp:gridview id="gridview1" runat="server" height="138px" width="1117px"
allowpaging="true" allowsorting="true" autogeneratecolumns="false"
onpageindexchanging="gridview1_pageindexchanging"
onrowcancelingedit="gridview1_rowcancelingedit"
onrowdeleting="gridview1_rowdeleting" onrowediting="gridview1_rowediting"
onrowupdating="gridview1_rowupdating" pagesize="4"
onrowdatabound="gridview1_rowdatabound">
<columns>
<asp:templatefield>
<itemtemplate>
<asp:checkbox id="checkbox1" runat="server" />
</itemtemplate>
</asp:templatefield>
<asp:boundfield datafield="pid" headertext="身份证号" sortexpression="pid" />
<asp:boundfield datafield="pname" headertext="姓名" sortexpression="pname" />
<asp:boundfield datafield="psex" headertext="性别" sortexpression="psex" />
<asp:boundfield datafield="padress" headertext="地址" sortexpression="padress" />
<asp:boundfield datafield="pyoubiao" headertext="邮编" sortexpression="pyoubiao" />
<asp:boundfield datafield="pprice" headertext="工资起价" sortexpression="pprice" />
<%-- <asp:commandfield headertext="选择" showselectbutton="true" />
<asp:commandfield headertext="编辑" showeditbutton="true" />
<asp:commandfield headertext="删除" showdeletebutton="true" />--%>
</columns>
<emptydatarowstyle backcolor="red" />
<headerstyle backcolor="#0000cc" font-bold="true" forecolor="white" />
<pagerstyle backcolor="#864" horizontalalign="center" forecolor="white"/>
</asp:gridview>
gridview.aspx.cs
复制代码 代码如下:
//公有数据
sqlconnection con = new sqlconnection(configurationmanager.connectionstrings["sqlcon"].connectionstring);
sqlcommand cmd;
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
bind(); //正常绑定
}
}
public void bind()
{
//数据连接web.config中已配置的数据
sqldataadapter da = new sqldataadapter("select * from person", con);//使用数据适配器内置查询,自动开闭数据库
dataset ds = new dataset();//使用数据集,采取断开式访问数据
da.fill(ds, "person");
gridview1.datasource = ds;
gridview1.datakeynames=new string[]{"pid"};//设置主键
gridview1.databind();
}
//鼠标移动到某一行,改变改行颜色
protected void gridview1_rowdatabound(object sender, gridviewroweventargs e)
{
for (int i = 0; i < gridview1.rows.count+1; i++) //执行循环,保证每条数据都可以更新
{
if (e.row.rowtype == datacontrolrowtype.datarow) //首先判断是否是数据行
{
//当鼠标停留时更改背景色
e.row.attributes.add("onmouseover", "c=this.style.backgroundcolor;this.style.backgroundcolor='#999'");
//当鼠标移开时还原背景色
e.row.attributes.add("onmouseout", "this.style.backgroundcolor=c");
}
}
}
运行结果:
鼠标移到gridview某一行时改变该行的背景色方法二
小实例:
gridview.aspx
gridview.aspx.cs
复制代码 代码如下:
//鼠标移动到某一行,改变改行颜色
protected void gridview1_rowdatabound(object sender, gridviewroweventargs e)
{
//如果是绑定数据行
if (e.row.rowtype == datacontrolrowtype.datarow)
{
//鼠标经过时,行背景色变
e.row.attributes.add("onmouseover", "this.style.backgroundcolor='#999'");
//鼠标移出时,行背景色变
e.row.attributes.add("onmouseout", "this.style.backgroundcolor='#087'");
}
}
运行结果:
gridview实现删除时弹出确认对话框
小实例:
gridview.aspx
复制代码 代码如下:
<asp:gridview id="gridview1" runat="server" height="138px" width="1117px"
allowpaging="true" allowsorting="true" autogeneratecolumns="false"
onpageindexchanging="gridview1_pageindexchanging"
onrowcancelingedit="gridview1_rowcancelingedit"
onrowdeleting="gridview1_rowdeleting" onrowediting="gridview1_rowediting"
onrowupdating="gridview1_rowupdating" pagesize="4"
onrowdatabound="gridview1_rowdatabound">
<columns>
<asp:templatefield>
<itemtemplate>
<asp:checkbox id="checkbox1" runat="server" />
</itemtemplate>
</asp:templatefield>
<asp:boundfield datafield="pid" headertext="身份证号" sortexpression="pid" />
<asp:boundfield datafield="pname" headertext="姓名" sortexpression="pname" />
<asp:boundfield datafield="psex" headertext="性别" sortexpression="psex" />
<asp:boundfield datafield="padress" headertext="地址" sortexpression="padress" />
<asp:boundfield datafield="pyoubiao" headertext="邮编" sortexpression="pyoubiao" />
<asp:boundfield datafield="pprice" headertext="工资起价" sortexpression="pprice" />
<%-- <asp:commandfield headertext="选择" showselectbutton="true" />
<asp:commandfield headertext="编辑" showeditbutton="true" />--%>
<asp:commandfield headertext="删除" showdeletebutton="true" />
</columns>
<emptydatarowstyle backcolor="red" />
<headerstyle backcolor="#0000cc" font-bold="true" forecolor="white" />
<pagerstyle backcolor="#864" horizontalalign="center" forecolor="white"/>
</asp:gridview>
gridview.aspx.cs
复制代码 代码如下:
//鼠标移动到某一行,改变改行颜色
protected void gridview1_rowdatabound(object sender, gridviewroweventargs e)
{
#region
//如果是绑定数据行
if (e.row.rowtype == datacontrolrowtype.datarow)
{
//鼠标经过时,行背景色变
e.row.attributes.add("onmouseover", "this.style.backgroundcolor='#999'");
//鼠标移出时,行背景色变
e.row.attributes.add("onmouseout", "this.style.backgroundcolor='#087'");
}
#endregion
//如果是绑定数据行
if (e.row.rowtype == datacontrolrowtype.datarow)
{
if (e.row.rowstate == datacontrolrowstate.normal || e.row.rowstate == datacontrolrowstate.alternate)
{
((linkbutton)e.row.cells[7].controls[0]).attributes.add("onclick", "javascript:return confirm('你确认要删除:\"" + e.row.cells[1].text + "\"吗?')");
}
}
}
运行结果:
gridview实现自动编号
小实例:
gridview.aspx
前台代码如上
gridview.aspx.cs
绑定显示数据代码如上,不再总结,以下只做主要代码:
复制代码 代码如下:
//实现自动编号
protected void gridview1_rowdatabound(object sender, gridviewroweventargs e)
{
#region
//如果是绑定数据行
if (e.row.rowtype == datacontrolrowtype.datarow)
{
//鼠标经过时,行背景色变
e.row.attributes.add("onmouseover", "this.style.backgroundcolor='#999'");
//鼠标移出时,行背景色变
e.row.attributes.add("onmouseout", "this.style.backgroundcolor='#087'");
}
#endregion
#region
//如果是绑定数据行
if (e.row.rowtype == datacontrolrowtype.datarow)
{
if (e.row.rowstate == datacontrolrowstate.normal || e.row.rowstate == datacontrolrowstate.alternate)
{
((linkbutton)e.row.cells[8].controls[0]).attributes.add("onclick", "javascript:return confirm('你确认要删除:\"" + e.row.cells[1].text + "\"吗?')");
}
}
#endregion
if (e.row.rowindex != -1)
{
int id = e.row.rowindex + 1;
e.row.cells[0].text = id.tostring();
}
}
运行结果:
gridview实现用“...”代替超长字符串
小实例:
gridview.aspx
复制代码 代码如下:
<asp:gridview id="gridview1" runat="server" height="138px" width="1117px"
allowpaging="true" allowsorting="true" autogeneratecolumns="false"
onpageindexchanging="gridview1_pageindexchanging"
onrowcancelingedit="gridview1_rowcancelingedit"
onrowdeleting="gridview1_rowdeleting" onrowediting="gridview1_rowediting"
onrowupdating="gridview1_rowupdating" pagesize="4"
onrowdatabound="gridview1_rowdatabound">
<columns>
<asp:boundfield datafield="id" headertext="证号" sortexpression="id" />
<asp:templatefield>
<itemtemplate>
<asp:checkbox id="checkbox1" runat="server" />
</itemtemplate>
</asp:templatefield>
<asp:boundfield datafield="pid" headertext="身份证号" sortexpression="pid" />
<asp:boundfield datafield="pname" headertext="姓名" sortexpression="pname" />
<asp:boundfield datafield="psex" headertext="性别" sortexpression="psex" />
<asp:boundfield datafield="padress" headertext="地址" sortexpression="padress" />
<asp:boundfield datafield="pyoubiao" headertext="邮编" sortexpression="pyoubiao" />
<asp:boundfield datafield="pprice" headertext="工资起价" sortexpression="pprice" />
<%-- <asp:commandfield headertext="选择" showselectbutton="true" />
<asp:commandfield headertext="编辑" showeditbutton="true" />--%>
<asp:commandfield headertext="删除" showdeletebutton="true" />
</columns>
<emptydatarowstyle backcolor="red" />
<headerstyle backcolor="#0000cc" font-bold="true" forecolor="white" />
<pagerstyle backcolor="#864" horizontalalign="center" forecolor="white"/>
</asp:gridview>
gridview.aspx.cs
复制代码 代码如下:
public void bind()
{
//数据连接web.config中已配置的数据
sqldataadapter da = new sqldataadapter("select * from person", con);//使用数据适配器内置查询,自动开闭数据库
dataset ds = new dataset();//使用数据集,采取断开式访问数据
da.fill(ds, "person");
gridview1.datasource = ds;
gridview1.datakeynames=new string[]{"pid"};//设置主键
gridview1.databind();
//省略字符
for (int i = 0; i <= gridview1.rows.count - 1; i++)
{
datarowview mydrv;
string gintro;
if (gridview1.pageindex == 0)
{
mydrv = ds.tables["person"].defaultview[i];
gintro = convert.tostring(mydrv["padress"]);
gridview1.rows[i].cells[5].text = substr(gintro, 2);
}
else
{
mydrv = ds.tables["person"].defaultview[i + (5 * gridview1.pageindex)];
gintro = convert.tostring(mydrv["padress"]);
gridview1.rows[i].cells[5].text = substr(gintro, 2);
}
}
}
public string substr(string sstring, int nleng)
{
if (sstring.length <= nleng)
{
return sstring;
}
string snewstr = sstring.substring(0, nleng);
snewstr = snewstr + "...";
return snewstr;
}
运行结果:
gridview显示隐藏某一列
小实例:
gridview.aspx
复制代码 代码如下:
<asp:gridview id="gridview1" runat="server" height="138px" width="1117px"
allowpaging="true" allowsorting="true" autogeneratecolumns="false"
onpageindexchanging="gridview1_pageindexchanging"
onrowcancelingedit="gridview1_rowcancelingedit"
onrowdeleting="gridview1_rowdeleting" onrowediting="gridview1_rowediting"
onrowupdating="gridview1_rowupdating" pagesize="4"
onrowdatabound="gridview1_rowdatabound">
<columns>
<asp:boundfield datafield="id" headertext="证号" sortexpression="id" />
<asp:templatefield>
<itemtemplate>
<asp:checkbox id="checkbox1" autopostback="true" runat="server" />
</itemtemplate>
</asp:templatefield>
<asp:boundfield datafield="pid" headertext="身份证号" sortexpression="pid" />
<asp:boundfield datafield="pname" headertext="姓名" sortexpression="pname" />
<asp:boundfield datafield="psex" headertext="性别" sortexpression="psex" />
<asp:boundfield datafield="padress" headertext="地址" sortexpression="padress" />
<asp:boundfield datafield="pyoubiao" headertext="邮编" sortexpression="pyoubiao" />
<asp:boundfield datafield="pprice" headertext="工资起价" sortexpression="pprice" />
<%-- <asp:commandfield headertext="选择" showselectbutton="true" />
<asp:commandfield headertext="编辑" showeditbutton="true" />--%>
<asp:commandfield headertext="删除" showdeletebutton="true" />
</columns>
<emptydatarowstyle backcolor="red" />
<headerstyle backcolor="#0000cc" font-bold="true" forecolor="white" />
<pagerstyle backcolor="#864" horizontalalign="center" forecolor="white"/>
</asp:gridview>
<br />
<asp:checkbox id="checkbox2" runat="server" forecolor="red" text="全选"
autopostback="true" oncheckedchanged="checkbox2_checkedchanged" />
<asp:checkbox id="checkbox3" runat="server" forecolor="red" text="隐藏工资起价"
autopostback="true" oncheckedchanged="checkbox3_checkedchanged" />
<asp:button id="button1" runat="server" font-bold="true" forecolor="red"
text="删除" onclick="button1_click" />
<asp:button id="button2" runat="server" font-bold="true" forecolor="red"
text="取消" onclick="button2_click" />
gridview.aspx.cs
复制代码 代码如下:
public void bind()
{
//数据连接web.config中已配置的数据
sqldataadapter da = new sqldataadapter("select * from person", con);//使用数据适配器内置查询,自动开闭数据库
dataset ds = new dataset();//使用数据集,采取断开式访问数据
da.fill(ds, "person");
gridview1.datasource = ds;
gridview1.datakeynames=new string[]{"pid"};//设置主键
gridview1.databind();
//省略字符
#region
for (int i = 0; i <= gridview1.rows.count - 1; i++)
{
datarowview mydrv;
string gintro;
if (gridview1.pageindex == 0)
{
mydrv = ds.tables["person"].defaultview[i];
gintro = convert.tostring(mydrv["padress"]);
gridview1.rows[i].cells[5].text = substr(gintro, 2);
}
else
{
mydrv = ds.tables["person"].defaultview[i + (5 * gridview1.pageindex)];
gintro = convert.tostring(mydrv["padress"]);
gridview1.rows[i].cells[5].text = substr(gintro, 2);
}
}
#endregion
//隐藏列
gridview1.columns[7].visible = false;//一开始隐藏
checkbox3.checked = false;//如果不这样后面的代码会把他true
}
//隐藏列
protected void checkbox3_checkedchanged(object sender, eventargs e)
{
gridview1.columns[7].visible = !gridview1.columns[7].visible;
response.write("gridview1的第8列现在的显示隐藏状态是:" + gridview1.columns[7].visible.tostring());
}
运行结果:
gridview弹出新页面/弹出新窗口
小实例:
gridview.aspx
运行结果:
gridview突出显示某一单元格(工资低于10000元)
小实例:
gridview.aspx
复制代码 代码如下:
<asp:gridview id="gridview1" runat="server" height="138px" width="1117px"
allowpaging="true" allowsorting="true" autogeneratecolumns="false"
onpageindexchanging="gridview1_pageindexchanging"
onrowcancelingedit="gridview1_rowcancelingedit"
onrowdeleting="gridview1_rowdeleting" onrowediting="gridview1_rowediting"
onrowupdating="gridview1_rowupdating" pagesize="4"
onrowdatabound="gridview1_rowdatabound">
<columns>
<asp:boundfield datafield="pid" headertext="身份证号" sortexpression="pid" />
<%-- <asp:boundfield datafield="pname" headertext="姓名" sortexpression="pname" />--%>
<asp:hyperlinkfield headertext="姓名" text="姓名" datanavigateurlfields="pname" datanavigateurlformatstring="webform1.aspx?goodsid={0}" target="mainframe" navigateurl="~/webform1.aspx" datatextfield="pname" >
</asp:hyperlinkfield>
<asp:boundfield datafield="psex" headertext="性别" sortexpression="psex" />
<asp:boundfield datafield="padress" headertext="地址" sortexpression="padress" />
<asp:boundfield datafield="pyoubiao" headertext="邮编" sortexpression="pyoubiao" />
<asp:boundfield datafield="pprice" headertext="工资起价" dataformatstring="{0:c}" sortexpression="pprice" />
<asp:commandfield headertext="选择" showselectbutton="true" />
<asp:commandfield headertext="编辑" showeditbutton="true" />
<asp:commandfield headertext="删除" showdeletebutton="true" />
</columns>
<emptydatarowstyle backcolor="red" />
<headerstyle backcolor="#0000cc" font-bold="true" forecolor="white" />
<pagerstyle backcolor="#864" horizontalalign="center" forecolor="white"/>
</asp:gridview>
gridview.aspx.cs
复制代码 代码如下:
public void bind()
{
//数据连接web.config中已配置的数据
sqldataadapter da = new sqldataadapter("select * from person", con);//使用数据适配器内置查询,自动开闭数据库
dataset ds = new dataset();//使用数据集,采取断开式访问数据
da.fill(ds, "person");
gridview1.datasource = ds;
gridview1.datakeynames=new string[]{"pid"};//设置主键
gridview1.databind();
//省略字符
#region
//for (int i = 0; i <= gridview1.rows.count - 1; i++)
//{
// datarowview mydrv;
// string gintro;
// if (gridview1.pageindex == 0)
// {
// mydrv = ds.tables["person"].defaultview[i];
// gintro = convert.tostring(mydrv["padress"]);
// gridview1.rows[i].cells[3].text = substr(gintro, 2);
// }
// else
// {
// mydrv = ds.tables["person"].defaultview[i + (5 * gridview1.pageindex)];
// gintro = convert.tostring(mydrv["padress"]);
// gridview1.rows[i].cells[5].text = substr(gintro, 2);
// }
//}
#endregion
//隐藏列
//gridview1.columns[3].visible = false;//一开始隐藏
//checkbox3.checked = false;//如果不这样后面的代码会把他true
//突出显示某一单元格(工资起价少于10000)
for (int i = 0; i <= gridview1.rows.count - 1; i++)
{
datarowview mydrv = ds.tables["person"].defaultview[i];
string price = convert.tostring(mydrv["pprice"]);
if (convert.todouble(price) < 10000)//大家这里根据具体情况设置可能toint32等等
{
gridview1.rows[i].cells[5].backcolor = system.drawing.color.red;
}
}
}
运行结果:
gridview数据导入excel/excel数据读入gridview
小实例:
gridview.aspx
复制代码 代码如下:
<asp:gridview id="gridview1" runat="server" height="138px" width="1117px"
allowpaging="true" allowsorting="true" autogeneratecolumns="false"
onpageindexchanging="gridview1_pageindexchanging"
onrowcancelingedit="gridview1_rowcancelingedit"
onrowdeleting="gridview1_rowdeleting" onrowediting="gridview1_rowediting"
onrowupdating="gridview1_rowupdating">
<columns>
<asp:templatefield headertext="身份证号" sortexpression="pid">
<itemtemplate>
<asp:label id="label1" runat="server" text='<%# bind("pid") %>'></asp:label>
</itemtemplate>
</asp:templatefield>
<%-- <asp:boundfield datafield="pid" headertext="身份证号" sortexpression="pid" />--%>
<asp:boundfield datafield="pname" headertext="姓名" sortexpression="pname" />
<asp:boundfield datafield="psex" headertext="性别" sortexpression="psex" />
<asp:boundfield datafield="padress" headertext="地址" sortexpression="padress" />
<asp:boundfield datafield="pyoubiao" headertext="邮编" sortexpression="pyoubiao" />
<asp:boundfield datafield="pprice" headertext="工资起价" sortexpression="pprice" />
<asp:commandfield headertext="选择" showselectbutton="true" />
<asp:commandfield headertext="编辑" showeditbutton="true" />
<asp:commandfield headertext="删除" showdeletebutton="true" />
</columns>
<emptydatarowstyle backcolor="red" />
<headerstyle backcolor="#0000cc" font-bold="true" forecolor="white" />
<pagerstyle backcolor="#864" horizontalalign="center" forecolor="white"/>
</asp:gridview>
gridview.aspx.cs
复制代码 代码如下:
//公有数据
sqlconnection con = new sqlconnection(configurationmanager.connectionstrings["sqlcon"].connectionstring);
sqlcommand cmd;
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
bind(); //正常绑定
}
}
/// <summary>
/// 主题:绑定数据,查询显示个人信息
/// 时间:2012年11月21日14:54:13
/// 描述:通过简单的方法绑定,实现数据显示
/// 作者:小伙
/// </summary>
public void bind()
{
//数据连接web.config中已配置的数据
sqldataadapter da = new sqldataadapter("select top 5 * from person", con);//使用数据适配器内置查询,自动开闭数据库
dataset ds = new dataset();//使用数据集,采取断开式访问数据
da.fill(ds, "person");
gridview1.datasource = ds;
gridview1.datakeynames=new string[]{"pid"};//设置主键
gridview1.databind();
}
//实现分页
protected void gridview1_pageindexchanging(object sender, gridviewpageeventargs e)
{
gridview1.pageindex = e.newpageindex;
this.bind();
}
//删除
protected void gridview1_rowdeleting(object sender, gridviewdeleteeventargs e)
{
string sqlstr = "delete from person where pid='" + gridview1.datakeys[e.rowindex].value.tostring() + "'";
con.open();
cmd = new sqlcommand(sqlstr, con);
cmd.executenonquery();
con.close();
bind();
}
//取消编辑
protected void gridview1_rowcancelingedit(object sender, gridviewcancelediteventargs e)
{
gridview1.editindex = -1;
bind();
}
//编辑
protected void gridview1_rowediting(object sender, gridviewediteventargs e)
{
gridview1.editindex = e.neweditindex;
bind();
}
//更新
protected void gridview1_rowupdating(object sender, gridviewupdateeventargs e)
{
string sqlstr = "update person set pname='"
+((textbox)(gridview1.rows[e.rowindex].cells[1].controls[0])).text.tostring().trim() + "',psex='"
+((textbox)(gridview1.rows[e.rowindex].cells[2].controls[0])).text.tostring().trim()+"',padress='"
+((textbox)(gridview1.rows[e.rowindex].cells[3].controls[0])).text.tostring().trim()+"',pyoubiao='"
+((textbox)(gridview1.rows[e.rowindex].cells[4].controls[0])).text.tostring().trim()+"',pprice='"
+((textbox)(gridview1.rows[e.rowindex].cells[5].controls[0])).text.tostring().trim()+"' where pid='"
+gridview1.datakeys[e.rowindex].value.tostring()+"'";
cmd = new sqlcommand(sqlstr, con);
con.open();
cmd.executenonquery();
con.close();
gridview1.editindex = -1;
bind();
}
运行结果:
导出结果: