(Original) An example of dynamically adding a template column to the DataGrid

Source: Internet
Author: User


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 here Code 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

{

// Second template Column

Public void instantiatein (control container)

{

Dropdownlist DPL = new dropdownlist ();

DPL. ID = "Dep ";

Container. Controls. Add (DPL );

// Add a drop-down box with ID = "Dep". The data is bound to itemdatabound of the DataGrid.

}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.