表dep:depid(标识主键),depname(学院名字)
表stu:stuid(标识主键),stuname(学生名字),studepid(学院id=表dep.depid)
前台:
<%@ page language=”c#” codebehind=”webform30.aspx.cs” autoeventwireup=”false” inherits=”csdn.webform30″ %>
<!doctype html public “-//w3c//dtd html 4.0 transitional//en” >
<html>
<head>
<title>webform30</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 ms_positioning=”gridlayout”>
<form id=”form1″ method=”post” runat=”server”>
</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>
/// webform30 的摘要说明。
/// </summary>
public class webform30 : system.web.ui.page
{
datagrid datagrid1=new datagrid();
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
createdatagrid();
}
protected void createdatagrid()
{
datagrid1.autogeneratecolumns=false;
datagrid1.cssclass=”border”;
datagrid1.borderwidth=0;
datagrid1.cellspacing=1;
datagrid1.cellpadding=5;
datagrid1.itemstyle.cssclass=”item”;
datagrid1.headerstyle.cssclass=”header”;
datagrid1.datakeyfield=”stuid”;
//以上设定datagrid的样式
templatecolumn tm=new templatecolumn();
tm.itemtemplate=new columntemplate1();
tm.headertext=”姓名”;
datagrid1.columns.add(tm);
//建立第一个模板列
templatecolumn tm2=new templatecolumn();
tm2.itemtemplate=new columntemplate2();
tm2.headertext=”学院”;
datagrid1.columns.add(tm2);
//建立第二个模板列
buttoncolumn bc=new buttoncolumn();
bc.buttontype=buttoncolumntype.pushbutton;
bc.commandname=”del”;
bc.text=”删除”;
datagrid1.columns.add(bc);
//建立删除按钮列
setbind();
//填充数据
page.controls[1].controls.add(datagrid1);
//给页面的form加入这个datagrid1
}
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();
}
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.item||e.item.itemtype==listitemtype.alternatingitem)
{
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;
}
}
private void datagrid1_itemcommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e)
{
if(e.commandname==”del”)
{
sqlconnection conn=new sqlconnection(system.configuration.configurationsettings.appsettings[“conn”]);
sqlcommand comm=new sqlcommand(“delete from stu where stuid=@id”,conn);
sqlparameter parm1=new sqlparameter(“@id”,sqldbtype.int);
parm1.value=this.datagrid1.datakeys[e.item.itemindex];
comm.parameters.add(parm1);
conn.open();
comm.executenonquery();
conn.close();
setbind();
}
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
this.datagrid1.itemdatabound += new system.web.ui.webcontrols.datagriditemeventhandler(this.datagrid1_itemdatabound);
this.datagrid1.itemcommand += new system.web.ui.webcontrols.datagridcommandeventhandler(this.datagrid1_itemcommand);//这里的两个事件千万别忘记,因为datagrid是后台创建的,这些事件需要自己来写上,vs.net也不会为你创建
}
#endregion
}
public class columntemplate1 : itemplate
{
//第一个模板列
public void instantiatein(control container)
{
literalcontrol l = new literalcontrol();
l.databinding += new eventhandler(this.ondatabinding);
//数据绑定
container.controls.add(l);
//为模板列加入literalcontrol
}
public void ondatabinding(object sender, eventargs e)
{
literalcontrol l = (literalcontrol) sender;//literalcontrol发送绑定请求
datagriditem container = (datagriditem) l.namingcontainer;
l.text = ((datarowview)container.dataitem)[“stuname”].tostring();//绑定stuname字段
}
}
public class columntemplate2 : itemplate
{
//第二个模板列
public void instantiatein(control container)
{
dropdownlist dpl = new dropdownlist();
dpl.id=”dep”;
container.controls.add(dpl);
//加入一个id=”dep”的下拉框,数据在datagrid的itemdatabound中绑定
}
}
}