Solved

gridview and controls on the fly

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

16 Experts available now in Live!

Get 1:1 Help Now