Example of dynamically adding a GridView template column in asp.net 2.0

Source: Internet
Author: User
Tags header tostring
Asp.net| News | templates

The key to dynamically adding columns is to implement the Itemplate.instantiatein method. The following is an example of adding a GridView template column.

C # code

<%...@ Page language= "C #"%>
<%...@ Import namespace= "System.Data"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"
"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

<script runat= "Server" ....
ICollection CreateDataSource ()
... {
DataTable dt = new DataTable ();
DataRow Dr;
Dt. Columns.Add (New DataColumn ("id", typeof (Int32));
Dt. Columns.Add (New DataColumn ("text", typeof (String));
for (int i = 0; i < 6; i++)
... {
Dr = dt. NewRow ();
Dr[0] = i;
DR[1] = "list item" + i.ToString ();
Dt. Rows.Add (DR);
}
DataView dv = new DataView (DT);
return DV;
}

public class Gridviewtemplate:itemplate
... {
Private DataControlRowType Templatetype;
private string ColumnName;

Public gridviewtemplate (DataControlRowType type, string colname)
... {
Templatetype = type;
ColumnName = colname;
}

public void InstantiateIn (System.Web.UI.Control container)
... {
Switch (templatetype)
... {
Case Datacontrolrowtype.header:
Literal LC = new Literal ();
Lc. Text = ColumnName;
Container. Controls.Add (LC);
Break
Case Datacontrolrowtype.datarow:
DropDownList DRR = new DropDownList ();
drr.id = "Dropdown";
Drr. AppendDataBoundItems = true;
Drr. Items.Add (New ListItem ("-----Please select------", ""));
Drr. Items.Add (New ListItem ("AA", "a"));
Drr. Items.Add (New ListItem ("BB", "B"));
Drr. Items.Add (New ListItem ("CC", "C"));
Container. Controls.Add (DRR);
Break
Default
Break
}
}
}

protected void Page_Load (object sender, EventArgs e)
... {
if (! IsPostBack)
... {
TemplateField CustomField = new TemplateField ();
Customfield.showheader = true;
Customfield.headertemplate = new Gridviewtemplate (Datacontrolrowtype.header, "dynamically adding columns");
Customfield.itemtemplate = new Gridviewtemplate (Datacontrolrowtype.datarow, "");
GRIDVIEW1.COLUMNS.ADD (CustomField);
Gridview1.datasource = CreateDataSource ();
Gridview1.databind ();
}
}

protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
... {
if (E.row.rowtype = = Datacontrolrowtype.datarow)
... {
Here you can access the value of the other fields of the database, you can set the default selection, specific application, to see their own play.
Here's an example, extrapolate, no longer crap.
DataRowView GV = (DataRowView) E.row.dataitem;
int itemseleted = Int32.Parse (GV. row["id"]. ToString ()) > 3? 0:int32.parse (GV). row["id"]. ToString ());
DropDownList dr = (DropDownList) e.row.findcontrol ("dropdown");
Dr. SelectedIndex = itemseleted;
}
}
</script>

<title>gridview examples of dynamically adding template columns </title>
<body>
<form id= "Form1" runat= "Server" >
<asp:gridview id= "GridView1" runat= "Server" autogeneratecolumns= "False"
onrowdatabound= "GridView1_RowDataBound" >
<Columns>
<asp:boundfield headertext= "title" datafield= "text"/>
</Columns>
</asp:GridView>
</form>
</body>




Related Article

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.