Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6759761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:59:18+00:00 2026-05-26T13:59:18+00:00

I need some help in rendering a dynamic gridview. Using the source code below,

  • 0

I need some help in rendering a dynamic gridview.
Using the source code below, I can add the grid to the page and it generates one row for each entry in the datasource.
The problem is that the controls only appears in the last row. If I add another entry to the datasource, I get one more row in the Gridview but only the last is showing the controls that I’ve added using the ItemTemplate.
When I debug the solution I can see that it instatiates a new ItemTemplate foreach collumn and that it adds all the controls, but after the databinding of the grid, all the cells are empty, except the ones of the last row.

Please help if you can. Thanks

private void BuildGridInterface()
{
    TBL_PARAM_01 xpto = new TBL_PARAM_01();
    GridView grid = new GridView();
    grid.ID = "grd";
    grid.AutoGenerateColumns = false;
    grid.ShowHeader = true;
    PropertyInfo[] props = xpto.GetType().GetProperties();

    foreach (PropertyInfo info in props)
    {
        TemplateField field = new TemplateField();

        object[] attributes = info.GetCustomAttributes(true);
        var att = attributes.SingleOrDefault(a => a.GetType() == typeof(Domain));
        WebControl control;

        if (att != null)
        {
            control = new DropDownList();
            control.ID = "drp" + info.Name;
            ((DropDownList)control).Items.Add(new ListItem() { Text = "XXXXX", Value = "1" });
            ((DropDownList)control).Items.Add(new ListItem() { Text = "XXXX2", Value = "2" });
            ((DropDownList)control).Items.Add(new ListItem() { Text = "XXXX3", Value = "3" });
            ((DropDownList)control).Items.Add(new ListItem() { Text = "XXXX4", Value = "4" });
            ((DropDownList)control).Items.Add(new ListItem() { Text = "XXXX5", Value = "5" });
            ((DropDownList)control).Items.Add(new ListItem() { Text = "XXXX6", Value = "6" });
         }
         else
         {
            control = new TextBox();
            control.ID = "txt" + info.Name;
         }

         field.ItemTemplate = new ItemTemplate(control, false, info.Name);                
         field.HeaderText = ((MatrixFieldLabel)attributes.Single(a => a.GetType() == typeof(MatrixFieldLabel))).Value;
         grid.Columns.Add(field);
    }

    FillGrid(grid);
    placer.Controls.Add(grid);
}

public class ItemTemplate : ITemplate
{
    WebControl Control { get; set; }
    bool Enabled { get; set; }
    string ColumName { get; set; }

    public ItemTemplate(WebControl control, bool enabled, string columname)
    {
        this.Control = control;
        this.Enabled = enabled;
        this.ColumName = columname;
        control.DataBinding += new EventHandler(Control_DataBinding);
    }

    public void InstantiateIn(Control container)
    {
        ((WebControl)container).Enabled = Enabled;
        container.Controls.Add(this.Control);
    }

   void Control_DataBinding(object sender, EventArgs e)
   {
       GridViewRow container = (GridViewRow)((Control)sender).NamingContainer;
       object dataValue = DataBinder.Eval(container.DataItem, this.ColumName);
       if (sender.GetType() == typeof(TextBox))
          ((TextBox)sender).Text = dataValue.ToString();
       else if (sender.GetType() == typeof(DropDownList))
          ((DropDownList)sender).SelectedValue = dataValue.ToString();
   }

    private void FillGrid(GridView target)
    {
        List<TBL_PARAM_01> list = new List<TBL_PARAM_01>();
        list.Add(new TBL_PARAM_01() { IDEntityRecordProduct = 1, IDEntityRecordX = 2, Output = "XPTO 3" });
        list.Add(new TBL_PARAM_01() { IDEntityRecordProduct = 2, IDEntityRecordX = 3, Output = "XPTO 4" });
        list.Add(new TBL_PARAM_01() { IDEntityRecordProduct = 3, IDEntityRecordX = 4, Output = "XPTO 5" });
        list.Add(new TBL_PARAM_01() { IDEntityRecordProduct = 4, IDEntityRecordX = 5, Output = "XPTO 6" });
        list.Add(new TBL_PARAM_01() { IDEntityRecordProduct = 5, IDEntityRecordX = 6, Output = "XPTO 7" });

        target.DataSource = list;
        target.DataBind();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:59:18+00:00Added an answer on May 26, 2026 at 1:59 pm

    Problem solved. I’ve had to rewrite the method InstantiateIn of my ItemTemplate class.
    Thanks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help to solve this. I have a gridview and inside the gridview
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
need some help! I'm trying to write some code in objective-c that requires part-of-speech
Need some help. This javascript code should set the target of the form and
Need some help with a #jQuery issue. Can someone look at this and tell
Need some help about with Memcache. I have created a class and want to
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help with this problem in implementing with XSLT, I had already implemented
Need some help gathering thoughts on this issue. Our team is moving ahead with
Need some help assigning a mouseover event to display some icons that start out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.