Solved

gridview and controls on the fly

Posted on 2008-10-06
3
484 Views
Last Modified: 2012-08-13
Hi Experts,
how can i create a gridview by code and put inside a control created on the fly as well?

for istance i wanna have a gridview with 3 image controls inside created by code (not in design mode).

Thanks
0
Comment
Question by:xtremereality
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
crazyman earned 500 total points
ID: 22653131
just new up a gridview and add it to the controls collection, but be sure you re-add the controls each time the page runs, you should do this in Page_Init or override CreateChildControls

see
http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx

You will need to create your own Template by implementing ITemplate
see
http://2000mph.blogspot.com/2008/05/programmatically-creating-gridview-with.html
0
 

Author Comment

by:xtremereality
ID: 22654632
Sorry can you provide me an example as i tried the links you gave me but i get some errors.
0
 

Author Comment

by:xtremereality
ID: 22654671
this is the class that implements ITemplate, i get some errors on the points with the arrows.
see below

public class GridViewTemplate : System.Web.UI.Page, ITemplate
{
ListItemType _templateType;
string _columnName;


public GridViewTemplate(ListItemType type, string columnName)
{
_templateType = type;
_groupName = columnName;
}


public void InstantiateIn(System.Web.UI.Control container)
{

switch (_templateType)
{

case ListItemType.Header:

break;

case ListItemType.Item:

Literal lc = new Literal();

lc.DataBinding += new EventHandler(this.lc_DataBinding);

container.Controls.Add(lc);

break;

case ListItemType.EditItem:

break;

case ListItemType.Footer:

break;

}
}


private void lc_DataBinding(Object sender, EventArgs e)
{

Literal lc = (Literal)sender;

GridViewRow row = (GridViewRow)lc.NamingContainer;

string propertyValue =  <------------------------------------------------------------ Get error here
DataBinder.Eval(row.DataItem, "PropertyName").ToString(); <--------------- And here

lc.Text = propertyValue;

}
}

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question