Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 488
  • Last Modified:

gridview and controls on the fly

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
xtremereality
Asked:
xtremereality
  • 2
1 Solution
 
crazymanCommented:
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
 
xtremerealityAuthor Commented:
Sorry can you provide me an example as i tried the links you gave me but i get some errors.
0
 
xtremerealityAuthor Commented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now