One example of dynamically adding template columns to the DataGrid

Source: Internet
Author: User
Tags visual studio
datagrid| Dynamic | Template data source structure with http://blog.csdn.net/lovecherry/archive/2005/03/24/328455.aspx
Table Dep:depid (identifies primary key), Depname (college name)
Table Stu:stuid (logo primary key), Stuname (student name), Studepid (college id= table Dep.depid)

Front desk:
<%@ 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 description of the WEBFORM30.
</summary>
public class WebForm30:System.Web.UI.Page
{
DataGrid datagrid1=new DataGrid ();
private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize 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 above
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= "College";
DATAGRID1.COLUMNS.ADD (TM2);
Create a 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 ();
Populating data
PAGE.CONTROLS[1]. Controls.Add (DATAGRID1);
Add this DataGrid1 to the form of the page
}

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)
{
Like the code in the example given above, bind the Drop-down box to the data and select the default
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")



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.