Code to add a GridView template Column

Source: Internet
Author: User

To dynamically add columns, the key 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 project" + 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 ("----- select ------",""));
Drr. Items. Add (new ListItem ("AA", ""));
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 add 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)
...{
// The values of other fields in the database can be accessed here. You can set the default options. The specific application depends on your use.
// The following is an example.
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>

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> example of dynamically adding a template column in The GridView </title>
</Head>
<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>
</Html>

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.