1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Getting Values from Dynamically Created Template Columns in Gridview

Discussion in 'C#' started by Iuric, May 29, 2008.

  1. #1
    Hello everybody.
    I'm trying to read values from template columns created dinamically.I' ve tried to use the following code: gridview.rows[selectedrow].cells[cellposition].controls[0] but it finds me just the bound field columns ,when it tries to read from a template column it throws me the following error:"Specified argument was out of the range of valid values."The code used to add template fields dinamically is:
    public void InsertSpecificControl(GridView grid, string ClassName,int selectedRow)
    {
    grid.Columns.Clear();
    int id = 0;
    String strType = "Model." + ClassName + ", Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
    Type type = Type.GetType(strType);
    PropertyInfo[] properties = type.GetProperties();
    foreach (PropertyInfo property in properties)
    {
    //Checking for visibility
    String strAttr = "Model.DisplayAttribute, Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
    System.Reflection.PropertyAttributes propAttributes = property.Attributes;
    object[] attributes = property.GetCustomAttributes(Type.GetType(strAttr), true);
    bool visible = true;
    if (attributes != null)
    if (attributes.GetLength(0) > 0)
    {
    DisplayAttribute attr = (DisplayAttribute)attributes[0];
    visible = attr.Visible;
    }

    //If it's not visible go on and do not bind column
    if (!visible)
    continue;

    if (property.PropertyType.Name == "String" && property.Name!="Gender")
    {
    BoundField field = new BoundField();
    field.DataField = property.Name;
    field.HeaderText = property.Name;
    grid.Columns.Add(field);
    }
    if (property.PropertyType.Name == "DateTime")
    {
    id++;
    TemplateField field = new TemplateField();
    field.HeaderText=property.Name;
    field.ItemTemplate = new AddTemplateToGridView(ListItemType.Item, property.Name,id,selectedRow);
    grid.Columns.Add(field);
    }
    if (property.Name == "Gender")
    {
    TemplateField field = new TemplateField();
    field.HeaderText = property.Name;
    field.ItemTemplate = new AddTemplateToGridView(ListItemType.AlternatingItem, property.Name,id,selectedRow);
    grid.Columns.Add(field);

    }

    }
    }
    }
    public class AddTemplateToGridView :ITemplate
    {
    ListItemType type;
    string colName;
    bool flag;
    int id;
    int rowNumber=0;
    int selectedRow;
    public AddTemplateToGridView(ListItemType type, string colname,int id,int selectedRow)
    {
    this.type = type;
    this.colName = colname;
    flag = false;
    this.id = id;
    this.selectedRow = selectedRow;
    }
    void ITemplate.InstantiateIn(System.Web.UI.Control container)
    {

    switch (type)
    {
    case ListItemType.Item:
    {
    id++;
    eWorld.UI.CalendarPopup calendar = new eWorld.UI.CalendarPopup();
    calendar.ID = "calendar"+id;
    calendar.DataBinding += new EventHandler(lb_DataBinding);
    container.Controls.Add(calendar);
    break;
    }
    case ListItemType.AlternatingItem:
    {
    RadioButtonList rbl = new RadioButtonList();
    rbl.ID = "radio";
    rbl.DataBinding += new EventHandler(rbl_DataBinding);
    container.Controls.Add(rbl);
    break;
    }

    }

    }

    void lb_DataBinding(object sender, EventArgs e)
    {

    eWorld.UI.CalendarPopup calendar = (eWorld.UI.CalendarPopup)sender;
    GridViewRow container = (GridViewRow)calendar.NamingContainer;
    object dataValue = DataBinder.Eval(container.DataItem, colName);
    if (dataValue != DBNull.Value)
    {
    if (rowNumber == selectedRow)
    {
    calendar.SelectedDate = DateTime.Parse(dataValue.ToString());
    calendar.Enabled = !flag;
    }
    else
    {
    calendar.SelectedDate = DateTime.Parse(dataValue.ToString());
    calendar.Enabled = flag;
    }
    }
    rowNumber++;
    }

    void rbl_DataBinding(object sender, EventArgs e)
    {
    RadioButtonList rbl = (RadioButtonList)sender;
    GridViewRow container = (GridViewRow)rbl.NamingContainer;
    object dataValue = DataBinder.Eval(container.DataItem, colName);
    if (dataValue != DBNull.Value)
    {
    rbl.Items.AddRange(new ListItem[]{ new ListItem("M"),new ListItem("F")});
    if (dataValue.ToString() == "m")
    rbl.Items[0].Selected = true;
    else
    rbl.Items[1].Selected = true;
    if (rowNumber == selectedRow)
    rbl.Enabled = !flag;
    else
    rbl.Enabled = flag;
    }
    }
    But how to access templated columns dinamically?
    If anyone knows the solution,let me know please.
    Thank you very much and have a nice day.
     
    Iuric, May 29, 2008 IP
  2. iconico

    iconico Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I can't tell when this is all happening in your code, but are you sure that the columns are generated yet? Try accessing the data in PreRender as the columns will most probably be created then.
     
    iconico, Jun 10, 2008 IP