有的时候我们需要
(1)在编辑的时候用下拉框选择,并且默认为数据库的内容
(2)使用下拉框过滤数据
(3)使用css统一定制datagrid
下面给出代码:
数据结构:
表dep:depid(标识主键),depname(学院名字)
表stu:stuid(标识主键),stuname(学生名字),studepid(学院id=表dep.depid)
前台:
<%@ page language=”c#” codebehind=”webform28.aspx.cs” autoeventwireup=”false” inherits=”csdn.webform28″ %>
<!doctype html public “-//w3c//dtd html 4.0 transitional//en” >
<html>
<head>
<title>webform28</title>
<meta name=”generator” content=”microsoft visual studio .net 7.1″>
<meta name=”code_language” content=”c#”>
<meta name=”vs_defaultclientscript” content=”javascript”>
<link href=”css.css” rel=”stylesheet” type=”text/css”>
<meta name=”vs_targetschema” content=”http://schemas.microsoft.com/intellisense/ie5″>
</head>
<body>
<form id=”form1″ method=”post” runat=”server”>
<asp:dropdownlist id=”dropdownlist1″ runat=”server” autopostback=”true”></asp:dropdownlist>
<asp:datagrid id=”datagrid1″ runat=”server” autogeneratecolumns=”false” cellspacing=”1″ borderwidth=”0px”
cellpadding=”5″ cssclass=”border” oneditcommand=”edit” oncancelcommand=”cancel” onupdatecommand=”update”
datakeyfield=”stuid”>
<itemstyle cssclass=”item”></itemstyle>
<headerstyle cssclass=”header”></headerstyle>
<columns>
<asp:templatecolumn headertext=”姓名”>
<itemtemplate>
<%# databinder.eval(container.dataitem,”stuname”) %>
</itemtemplate>
<edititemtemplate>
<asp:textbox id=”name” runat=”server” text=<%# databinder.eval(container.dataitem,”stuname”) %> width=”88px”>
</asp:textbox>
</edititemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext=”学院”>
<itemtemplate>
<%# databinder.eval(container.dataitem,”depname”) %>
</itemtemplate>
<edititemtemplate>
<asp:dropdownlist id=”dep” runat=”server”></asp:dropdownlist>
</edititemtemplate>
</asp:templatecolumn>
<asp:editcommandcolumn buttontype=”pushbutton” updatetext=”更新” canceltext=”取消” edittext=”编辑”></asp:editcommandcolumn>
</columns>
</asp:datagrid>
</form>
</body>
</html>
后台:
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.data.sqlclient;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace csdn
{
/// <summary>
/// webform28 的摘要说明。
/// </summary>
public class webform28 : system.web.ui.page
{
protected system.web.ui.webcontrols.dropdownlist dropdownlist1;
protected system.web.ui.webcontrols.datagrid datagrid1;
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
if(!ispostback)
{
setbind();
setbind2();
}
}
protected void setbind()
{
sqlconnection conn=new sqlconnection(system.configuration.configurationsettings.appsettings[“conn”]);
sqldataadapter da=new sqldataadapter(“select * from stu,dep where stu.studepid=dep.depid”,conn);
dataset ds=new dataset();
da.fill(ds,”table1″);
this.datagrid1.datasource=ds.tables[“table1”];
this.datagrid1.databind();
}
protected void setbind2()
{
sqlconnection conn2=new sqlconnection(system.configuration.configurationsettings.appsettings[“conn”]);
sqldataadapter da2=new sqldataadapter(“select * from dep”,conn2);
dataset ds2=new dataset();
da2.fill(ds2,”table1″);
this.dropdownlist1.datasource=ds2.tables[“table1”];
this.dropdownlist1.datatextfield=”depname”;
this.dropdownlist1.datavaluefield=”depid”;
this.dropdownlist1.databind();
this.dropdownlist1.items.insert(0,new listitem(“请选择”,””));
}
protected void setbind3()
{
string s=this.dropdownlist1.selectedvalue;
sqlconnection conn=new sqlconnection(system.configuration.configurationsettings.appsettings[“conn”]);
sqlcommand comm=new sqlcommand();
comm.connection=conn;
if(s!=””)
{
comm.commandtext=”select * from stu,dep where stu.studepid=dep.depid and depid=@depid”;
sqlparameter parm1=new sqlparameter(“@depid”,sqldbtype.int);
parm1.value=s;
comm.parameters.add(parm1);
}
else
comm.commandtext=”select * from stu,dep where stu.studepid=dep.depid”;
sqldataadapter da=new sqldataadapter();
da.selectcommand=comm;
dataset ds=new dataset();
da.fill(ds,”table1″);
this.datagrid1.datasource=ds.tables[“table1”];
this.datagrid1.databind();
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.datagrid1.itemdatabound += new system.web.ui.webcontrols.datagriditemeventhandler(this.datagrid1_itemdatabound);
this.dropdownlist1.selectedindexchanged += new system.eventhandler(this.dropdownlist1_selectedindexchanged);
this.load += new system.eventhandler(this.page_load);
}
#endregion
private void datagrid1_itemdatabound(object sender, system.web.ui.webcontrols.datagriditemeventargs e)
{
sqlconnection conn=new sqlconnection(system.configuration.configurationsettings.appsettings[“conn”]);
sqldataadapter da=new sqldataadapter(“select * from dep”,conn);
dataset ds=new dataset();
da.fill(ds,”table1″);
if(e.item.itemtype==listitemtype.edititem)
{
dropdownlist ddl=(dropdownlist)e.item.findcontrol(“dep”);
ddl.datasource=ds.tables[“table1”];
ddl.datatextfield=”depname”;
ddl.datavaluefield=”depid”;
ddl.databind();
ddl.items.findbyvalue(convert.tostring(databinder.eval(e.item.dataitem,”depid”))).selected=true;//选择数据库内的作为默认
}
}
protected void edit(object sender,datagridcommandeventargs e)
{
this.datagrid1.edititemindex=e.item.itemindex;
if(this.dropdownlist1.selectedvalue==””)
setbind();
else
setbind3();
}
protected void cancel(object sender,datagridcommandeventargs e)
{
this.datagrid1.edititemindex=-1;
if(this.dropdownlist1.selectedvalue==””)
setbind();
else
setbind3();
}
protected void update(object sender,datagridcommandeventargs e)
{
if(e.item.itemtype==listitemtype.edititem)//只有在编辑按下以后才能提交
{
sqlconnection conn=new sqlconnection(system.configuration.configurationsettings.appsettings[“conn”]);
sqlcommand comm=new sqlcommand(“update stu set stuname=@name,studepid=@depid where stuid=@id”,conn);
sqlparameter parm1=new sqlparameter(“@name”,sqldbtype.nvarchar,50);
parm1.value=((textbox)e.item.findcontrol(“name”)).text;
sqlparameter parm2=new sqlparameter(“@depid”,sqldbtype.int);
parm2.value=((dropdownlist)e.item.findcontrol(“dep”)).selectedvalue;
sqlparameter parm3=new sqlparameter(“@id”,sqldbtype.int);
parm3.value=this.datagrid1.datakeys[e.item.itemindex];
comm.parameters.add(parm1);
comm.parameters.add(parm2);
comm.parameters.add(parm3);
conn.open();
comm.executenonquery();
conn.close();
this.datagrid1.edititemindex=-1;
if(this.dropdownlist1.selectedvalue==””)
setbind();
else
setbind3();//如果选择过滤则使用setbind3()
}
}
private void dropdownlist1_selectedindexchanged(object sender, system.eventargs e)
{
setbind3();
}
}
}
css:
.border {
background-color: #00496c;
}
.header {
font-family: “宋体”, sans-serif;
font-size: 10pt;
font-weight: bold;
color: #ffffff;
background-color: #0080c0;
text-align: center;
}
.item {
font-family: “宋体”, sans-serif;
font-size: 9pt;
font-weight: normal;
color: #0080c0;
background-color: #ffffff;
text-align: center;
}
代码比较简单,下面简单说明一下:
(1)setbind()是基本的绑定;setbind2()是绑定外面的那个dropdownlist;setbind3()是在下拉框选择了以后过滤后的datagrid的绑定
(2)这里使用css来实现表格边框是利用cellspacing,所以这个数值就是边框的宽度,在表格边框的css中使用background-color来描述边框的颜色。
下面是本站datagrid的一些索引:
datagrid实现增删(带提示)改和分页
绑定后动态改变datagrid中的内容
datagrid中的链接设置多个参数
datagrid实现自增列、单选、多选
datagrid数据导出到excel文件给客户端下载的几种方法
datagrid和dropdownlist的一些配合以及使用css定制datagrid