Programmatically Add Controls to GridView with AutoGenerateColumns=true?

Last post 06-11-2008 10:30 PM by mellamokb. 7 replies.

Sort Posts:

  • Programmatically Add Controls to GridView with AutoGenerateColumns=true?

    06-11-2008, 6:40 PM
    • Member
      35 point Member
    • neostile
    • Member since 06-15-2006, 3:45 AM
    • Posts 14

    Hi,

    Can you programmatically add controls to gridviews with AutoGenerateColumns=true?  I have tried doing it a couple different ways, but have not been able to see the controls on the gridview.  Below is an example of the way I have tried to add controls.  I have to use AutoGenerateColumns=true because I am populating the gridview from a stored procedure which creates a PivotTable/Crosstab.

    protected void GridView1_DataBound(object sender, EventArgs e)

    { 

    for (int i = 0; i < thisGridView.Rows.Count; i++)

    {

    for (int j = 0; j < thisGridView.Rows[i].Cells.Count; j++)

    {

    Label test = new Label();

    test.Text = "test";

    thisGridView.Rows[i].Cells[j].Controls.Add(test);

    }

    }

    }

  • Re: Programmatically Add Controls to GridView with AutoGenerateColumns=true?

    06-11-2008, 6:57 PM
    • All-Star
      28,218 point All-Star
    • johram
    • Member since 06-13-2006, 10:36 AM
    • Sweden
    • Posts 3,543
    • Moderator

    Why do you need to have AutoGenerateColumns=true if you then go and add them yourself?

    AFAIK, you cannot combine these two as AutoGenerateColumns=true most likely will clear the columns collection.

    If this post was useful to you, please mark it as answer. Thank you!
  • Re: Programmatically Add Controls to GridView with AutoGenerateColumns=true?

    06-11-2008, 7:03 PM
    • Member
      35 point Member
    • neostile
    • Member since 06-15-2006, 3:45 AM
    • Posts 14

    When setting it to true the store procedure generates the columns count.  I am using a stored procedure to create a crosstab which has a variable number of columns.  That is why using autogeneratecolumns is very useful.  I could programmatically build the gridview with loops adding rows and columns manually, but I would like to avoid the dev time.

  • Re: Programmatically Add Controls to GridView with AutoGenerateColumns=true?

    06-11-2008, 7:13 PM
    • Contributor
      3,561 point Contributor
    • mellamokb
    • Member since 02-09-2007, 5:44 PM
    • Posts 644

    Hi,

    I do not understand: if you say AutoGenerateColumns="true" and you run absolutely know code otherwise except to bind the GridView to your crosstab query stored procedure, what is wrong with the resulting GridView that you would like changed?

    Regards,

    ~ mellamokb

  • Re: Programmatically Add Controls to GridView with AutoGenerateColumns=true?

    06-11-2008, 7:27 PM
    • Member
      35 point Member
    • neostile
    • Member since 06-15-2006, 3:45 AM
    • Posts 14

    I need to add at the very least a button to each cell in the gridview

  • Re: Programmatically Add Controls to GridView with AutoGenerateColumns=true?

    06-11-2008, 7:32 PM
    • Contributor
      3,561 point Contributor
    • mellamokb
    • Member since 02-09-2007, 5:44 PM
    • Posts 644

    Hi,

    So leave your GridView with AutoGenerateColumns="true" and on the asp.net code page add your button: 

            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"
                DataSourceID="THE_DATA_SOURCE">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="Button1" runat="server" Text="BUTTON_TEXT" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
    

     Regards,

    ~ mellamokb

  • Re: Programmatically Add Controls to GridView with AutoGenerateColumns=true?

    06-11-2008, 8:07 PM
    • Member
      35 point Member
    • neostile
    • Member since 06-15-2006, 3:45 AM
    • Posts 14

    C'mon, the button has to be within each cell not to the left of every row.  Please don't make random suggestions.

  • Re: Programmatically Add Controls to GridView with AutoGenerateColumns=true?

    06-11-2008, 10:30 PM
    Answer
    • Contributor
      3,561 point Contributor
    • mellamokb
    • Member since 02-09-2007, 5:44 PM
    • Posts 644

    I am very sorry. Please accept my humble apologies, as I misread your post.

    Might I suggest to loop through all of the cells on the RowDataBound event of the GridView for each row, and append a Button control into the cell.  You will also need to take the text of the Cell and store it in a label so it is not replaced by the button.  This code worked for me: 

            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                Button b;
                Label l;
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    foreach (TableCell tc in e.Row.Cells)
                    {
                        l = new Label();
                        l.Text = tc.Text;
                        b = new Button();
                        b.Text = "Button Text";
                        tc.Controls.Add(l);
                        tc.Controls.Add(b);
                    }
                }
            }

     Please let me know what else I can do for you.

     Regards,

    ~ mellamokb

Page 1 of 1 (8 items)