Problem understanding how to create a dynamic GridView in C#

Discussion in 'ASP .Net Datagrid Control' started by PeterKellner, May 19, 2006.

  1. PeterKellner

    PeterKellner Guest

    I'm trying to create a gridview dynamically (in code from an
    ObjectDataSource). When I create it in the aspx page and dynamically
    add the datasource it works correctly. When I create it 100% in code
    (no asp:GridView), the headers is created, the number of rows shown is
    correct, but all the rows show empty without any controls.

    What Am I missing?

    Thanks

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    loadDynamicGridx();
    }

    //
    private void loadDynamicGridx()
    {

    // if the new GridView() control is replaced with the
    following in an
    // aspx page, the grid gets poplulated as exptected. If the
    gridview
    // is created dynamically, the headers get set, the rows are
    listed
    // but they are all empty of controls or data.
    // <asp:GridView ID="GrdDynamic" runat="server"
    ShowHeader="true">
    // </asp:GridView>
    //
    GridView GrdDynamic = new GridView();

    ObjectDataSource ods = new
    ObjectDataSource("MembershipUtilities.MembershipUserODS",
    "GetMembers");

    //Initialize the DataSource
    GrdDynamic.DataSource = ods;

    //Bind the datatable with the GridView.
    GrdDynamic.DataBind();


    }
    Peter Kellner
    http://peterkellner.net
    PeterKellner, May 19, 2006
    #1
    1. Advertising

  2. you need to have the <asp:GridView runat="server" id="yourGrid" /> in your
    code, after that you can do whatever you want with it dynamically... add
    columns, header, footer, styles, etc...

    if you do not have any datasource for the yourGrid, and if you do not use
    EmptyData property then is the same as visible="false"

    so, you can dynamically build whaever you want for the GridView, but you
    need to place the asp tag in your code, even if you do not show anything in
    it....

    as far as I know it's like that, but I will lokk futher the question in the
    we trying to find you if you can add an asp tag programattically, in mean
    while, use my advice :)

    --

    Bruno Alexandre
    (a Portuguese in KĂžbenhanv, Danmark)


    "PeterKellner" <> escreveu na mensagem
    news:...
    > I'm trying to create a gridview dynamically (in code from an
    > ObjectDataSource). When I create it in the aspx page and dynamically
    > add the datasource it works correctly. When I create it 100% in code
    > (no asp:GridView), the headers is created, the number of rows shown is
    > correct, but all the rows show empty without any controls.
    >
    > What Am I missing?
    >
    > Thanks
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if (!IsPostBack)
    > loadDynamicGridx();
    > }
    >
    > //
    > private void loadDynamicGridx()
    > {
    >
    > // if the new GridView() control is replaced with the
    > following in an
    > // aspx page, the grid gets poplulated as exptected. If the
    > gridview
    > // is created dynamically, the headers get set, the rows are
    > listed
    > // but they are all empty of controls or data.
    > // <asp:GridView ID="GrdDynamic" runat="server"
    > ShowHeader="true">
    > // </asp:GridView>
    > //
    > GridView GrdDynamic = new GridView();
    >
    > ObjectDataSource ods = new
    > ObjectDataSource("MembershipUtilities.MembershipUserODS",
    > "GetMembers");
    >
    > //Initialize the DataSource
    > GrdDynamic.DataSource = ods;
    >
    > //Bind the datatable with the GridView.
    > GrdDynamic.DataBind();
    >
    >
    > }
    > Peter Kellner
    > http://peterkellner.net
    Bruno Alexandre, May 19, 2006
    #2
    1. Advertising

  3. PeterKellner

    PeterKellner Guest

    On Thu, 18 May 2006 17:27:20 -0700, PeterKellner
    <> wrote:

    >I'm trying to create a gridview dynamically (in code from an
    >ObjectDataSource). When I create it in the aspx page and dynamically
    >add the datasource it works correctly. When I create it 100% in code
    >(no asp:GridView), the headers is created, the number of rows shown is
    >correct, but all the rows show empty without any controls.
    >
    >What Am I missing?
    >
    >Thanks
    >
    >protected void Page_Load(object sender, EventArgs e)
    > {
    > if (!IsPostBack)
    > loadDynamicGridx();
    > }
    >
    > //
    > private void loadDynamicGridx()
    > {
    >
    > // if the new GridView() control is replaced with the
    >following in an
    > // aspx page, the grid gets poplulated as exptected. If the
    >gridview
    > // is created dynamically, the headers get set, the rows are
    >listed
    > // but they are all empty of controls or data.
    > // <asp:GridView ID="GrdDynamic" runat="server"
    >ShowHeader="true">
    > // </asp:GridView>
    > //
    > GridView GrdDynamic = new GridView();
    >
    > ObjectDataSource ods = new
    >ObjectDataSource("MembershipUtilities.MembershipUserODS",
    >"GetMembers");
    >
    > //Initialize the DataSource
    > GrdDynamic.DataSource = ods;
    >
    > //Bind the datatable with the GridView.
    > GrdDynamic.DataBind();
    >
    >
    > }
    >Peter Kellner
    >http://peterkellner.net



    The two things I was missing was GrdDynamic needed to be declared
    protected and I needed to add the control to the form.

    form1.Controls.Add(GrdDynamic);

    Peter Kellner
    http://peterkellner.net
    PeterKellner, May 19, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike P
    Replies:
    0
    Views:
    596
    Mike P
    Aug 3, 2006
  2. =?Utf-8?B?Sm9obks=?=
    Replies:
    1
    Views:
    274
    =?Utf-8?B?Sm9obks=?=
    Oct 3, 2006
  3. Francesco S. Carta
    Replies:
    14
    Views:
    516
    Francesco S. Carta
    Aug 31, 2010
  4. bsm

    Dynamic GridView with Dynamic Column

    bsm, May 15, 2007, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    832
  5. PeterKellner
    Replies:
    1
    Views:
    187
    mou piya
    Jun 2, 2006
Loading...

Share This Page