C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Dynamically Add Controls to Grid.


Posted Date: 04 Jun 2008    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: BharathiMember Level: Diamond    
Rating: Points: 10



Please find the sample code for dynamically adding controls to gridview.

The datatable is also created dynamically for code clarity. And a Two column is added , once is TextBox and other is a Dropdownlist column. The same can be applied for other controls also.

If any events should be added to the control , then the "Attribute" property of the control will be used like:


Control1.Attributes.Add("","");

protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("City");
DataRow dr;
for (int i = 0; i < 5; i++)
{
dr = dt.NewRow();
dr[0] = "Name " + (i + 1);
dr[1] = "City " + (i + 1);
dt.Rows.Add(dr);
}
grd.DataSource = dt;
grd.DataBind();
}

protected void OnDataBound(object sender, GridViewRowEventArgs e)
{
string STR = e.Row.Cells[0].Text;

TableCell TC = new TableCell();
e.Row.Cells.Add(TC);
TC = new TableCell();
e.Row.Cells.Add(TC);

if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[2].Text = "Text Box";
e.Row.Cells[3].Text = "DropDownList";
}
else
{
TextBox txt = new TextBox();
txt.ID = "TEST";
e.Row.Cells[2].Controls.Add(txt);

DropDownList ddl = new DropDownList();
ddl.ID = "ddl";
e.Row.Cells[3].Controls.Add(ddl);
}
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Bind GridView to Array
Previous Resource: How Dyanmically bind a child gridview to the parent Grid.
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use