Data Source Structure with http://www.cnblogs.com/lovecherry/archive/2005/03/25/125525.html
Table Dep: depid (primary key ID), depname (school name)
Table STU: stuid (primary key ID), stuname (Student name), studepid (school ID = TABLE dep. depid)
Front-end:
<% @ 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>
Background:
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>
/// Summary of webform30.
/// </Summary>
Public class webform30: system. Web. UI. Page
{
DataGrid datagrid1 = new DataGrid ();
Private void page_load (Object sender, system. eventargs E)
{
// Place the user hereCode To initialize the page
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 ";
// Set the DataGrid style.
Templatecolumn TM = new templatecolumn ();
TM. itemtemplate = new columntemplate1 ();
TM. headertext = "name ";
Datagrid1.columns. Add (TM );
// Create the first template Column
Templatecolumn TM2 = new templatecolumn ();
Tm2.itemtemplate = new columntemplate2 ();
Tm2.headertext = "";
Datagrid1.columns. Add (TM2 );
// Create the second template Column
Buttoncolumn BC = new buttoncolumn ();
BC. buttontype = buttoncolumntype. Pushbutton;
BC. commandname = "Del ";
BC. Text = "delete ";
Datagrid1.columns. Add (BC );
// Create a delete button Column
Setbind ();
// Fill in data
Page. controls [1]. Controls. Add (datagrid1 );
// Add this datagrid1 to the form on the page
}
Protected void setbind ()
{
Sqlconnection conn = new sqlconnection (system. configuration. configurationsettings. deleettings ["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 maid (Object sender, system. Web. UI. webcontrols. datagriditemeventargs E)
{
// Bind data to the drop-down box and select the default
Sqlconnection conn = new sqlconnection (system. configuration. configurationsettings. deleettings ["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. deleettings ["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 ();
}
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
This. Maid + = new system. Web. UI. webcontrols. Maid (this. Maid );
This. datagrid1.itemcommand + = new system. web. UI. webcontrols. datagridcommandeventhandler (this. datagrid1_itemcommand); // do not forget the two events here, because the DataGrid is created in the background, these events need to be written by yourself, and vs.net will not be created for you
}
# Endregion
}
Public class columntemplate1: itemplate
{
// The first template Column
Public void instantiatein (control container)
{
Literalcontrol L = new literalcontrol ();
L. databinding + = new eventhandler (this. ondatabinding );
// Data Binding
Container. Controls. Add (L );
// Add literalcontrol to the template Column
}
Public void ondatabinding (Object sender, eventargs E)
{
Literalcontrol L = (literalcontrol) sender; // literalcontrol sends the Binding Request
Datagriditem Container = (datagriditem) L. namingcontainer;
L. Text = (datarowview) container. dataitem) ["stuname"]. tostring (); // bind the stuname Field
}
}
public class columntemplate2: itemplate
{< br> // second template column
Public void instantiatein (control container)
{< br> dropdownlist DPL = new dropdownlist ();
DPL. id = "Dep";
container. controls. add (DPL);
// Add an id = "Dep" drop-down box, bind data to the itemdatabound of the DataGrid
}< BR >}