Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Programmatically adding Control in GridView Cell (Controls.Add)

Author: DevASP Team
Download Source Code :

In this article I will try to explain you how you can add control in gridview cell programmatically.

Often as developer we face scenario like need of new control while running the application or developer wants to display record set in gridview cell by retrieving data from database. In aspx drop a gridview control and label control. The page will be like as follows:


<asp:GridView ID="gv" Visible="true" runat="server" AutoGenerateColumns="False">


                    <asp:TemplateField HeaderText="Controls">


                            <asp:Label ID="lbl" runat="server" Text="DropDownControl" ></asp:Label>





            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />


On page load bind the gridview control with arraylist. So that gridview will display on your web form.


    protected void Page_Load(object sender, EventArgs e)


        ArrayList al = new ArrayList();


        gv.DataSource = al;




The trick to adding the control to cell is on button click event the code for adding the control is as follows:


    protected void Button1_Click(object sender, EventArgs e)


        DropDownList ddl = new DropDownList();

        ddl.Visible = true;

        ddl.ID = "ddl1";


        TableCell cell = new TableCell();      





In above sample I have simply uses the Controls.Add property which adds the control in cell.

Article Comments
Hi!!! I need some similiar, I need to merge to columns, one column show the year(ex. 2008) and other the month(ex. 1=) I need to join both and present de information like (january 2008)

maybe you can help me

Posted on 1/23/2008 5:24:11 PM by Rogelio Leyva

in gridview where your binding your colums in template field do it like this

DataBinder.Eval(Container.DataItem, "months").ToString() + " " + DataBinder.Eval(Container.DataItem, "years").ToString()

Posted on 2/3/2008 5:38:14 AM by Thomas

Thank you. This article has really helped me. I was looking for the same thing for my project.

Posted on 2/21/2008 12:16:29 AM by Anand

This article has really helped me. I was looking for the same thing for my project.

Posted on 4/21/2008 10:54:03 AM by Susanta

Thank you....It helped me lot

Posted on 4/25/2008 7:16:50 AM by Sagar

I use that, but it doesn't view the list in Gridview,
it view
(System.Web.UI.WebControls.TableRow) in Grid instead of list

Posted on 1/5/2010 1:19:06 PM by Amal

This is an excellent article, thank you very much for it

Posted on 5/25/2010 7:14:20 PM by

Add Article Comment:
Name :
Email Address :
Comments :
<< How to Bind ArrayList to DataList Control

Disclaimer - Privacy
© 2002-2017