Problem understanding how to create a dynamic GridView in C#

P

PeterKellner

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
 
B

Bruno Alexandre

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 :)
 
P

PeterKellner

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top