Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Problem understanding how to create a dynamic GridView in C#

Reply
Thread Tools

Problem understanding how to create a dynamic GridView in C#

 
 
PeterKellner
Guest
Posts: n/a
 
      05-19-2006
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.MembershipUs erODS",
"GetMembers");

//Initialize the DataSource
GrdDynamic.DataSource = ods;

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


}
Peter Kellner
http://peterkellner.net
 
Reply With Quote
 
 
 
 
Bruno Alexandre
Guest
Posts: n/a
 
      05-19-2006
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" <(E-Mail Removed)> escreveu na mensagem
news:(E-Mail Removed)...
> 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.MembershipUs erODS",
> "GetMembers");
>
> //Initialize the DataSource
> GrdDynamic.DataSource = ods;
>
> //Bind the datatable with the GridView.
> GrdDynamic.DataBind();
>
>
> }
> Peter Kellner
> http://peterkellner.net



 
Reply With Quote
 
 
 
 
PeterKellner
Guest
Posts: n/a
 
      05-19-2006
On Thu, 18 May 2006 17:27:20 -0700, PeterKellner
<(E-Mail Removed)> 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.MembershipU serODS",
>"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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Dynamic memory management, straightening my understanding of itsdetails Francesco S. Carta C++ 14 08-31-2010 08:56 PM
Dynamic GridView with Dynamic Column bsm ASP .Net Building Controls 1 05-15-2007 01:57 PM
Problem understanding how to make a dynamic GridView execute Update on SqlDataSource PeterKellner ASP .Net Datagrid Control 1 06-02-2006 11:59 AM
GridView Hierarchical View - Gridview in Gridview =?Utf-8?B?bWdvbnphbGVzMw==?= ASP .Net 1 05-09-2006 06:48 PM



Advertisments