Solved

gridview and controls on the fly

Posted on 2008-10-06
3
481 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now