views:

4383

answers:

5

Hey,

I need to create a gridview based on 2 different datasources: main and sub-cathegory. And I need to list them like below:

Productinfo
   sub-product 1
   sub-product 2

Productinfo
   sub-product 1
   sub-product 2
   sub-product 3
   sub-product 4

Etc... the thing is that both the "productinfo" and the "sub-product" are dynamic as the number of both can vary, so I would have to create a gridview within a gridview, plus the necessary filters too.

For this reason I thought it was best to do it all in code-behind, but I can't understand how to use the gridview-class in codebehind and bind it so that it actually shows something in the main aspx page.

Basically what I'm asking for is a simple example of how, when you have nothing but <asp:GridView/> in the aspx -page, can you add components to it and show it, from code-behind (vb)?

Thanks.

A: 

I'm not sure i understand your question? ..but do you just want to databind your grid?

I would properly create the gridview in code-behind like so:

Dim gw As GridView = New GridView()

Or you need to add an Id and runat="server" to your asp:GridView to be able to use it in code-behind.

And when you need to get component into your gridview you need to bind a datasource, for example a generic list.

Dim list As List(Of String) = New List(Of String)
gw.DataSource = list
gw.DataBind()

If you want your main and subs lined up like you show, i would use 2 grids?

Frost
A: 

I just want to use the GridView from codebehind, but I don't know how to do that. And I don't understand enough of the various examples I've found either. I've already done this with tables and adding checkbox controls to them dynamically, for example, but this is different.

To put it very simple:

.aspx

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
   <Columns>
   </Columns>
</asp:GridView>


.apxs.vb Dim dt As DataTable = New DataTable() dt.Columns.Add("ID") dt.Columns.Add("Type") dt.Columns.Add("Name") GridView1.DataSource = dt GridView1.DataBind()

Obviously the code above doesn't do anything. Instead, I'd like for it to actually show the columns I created. What's wrong?

A: 

Hopefully you have already figured this out, but for the benefit of others here's what I've found.

First, ASP.NET doesn't render the gridview at all if there isn't any data... annoying, but what can you do. (Actually, there probably is something you can do, but I don't know what it is!). Add a new row to your datatable and you will be a step closer.

Second, you are specifying the columns for your datatable, not the gridview, so you want to have AutoGenerateColumns="True" so the gridview will pick up the column names from the table when it generates its columns.

HTH, Dave

A: 

vb code: Dim mydatatable As New DataTable ' Create columns mydatatable.Columns.Add("field_a", Type.GetType("System.String")) mydatatable.Columns.Add("field_b", Type.GetType("System.String")) ' Declare row Dim myrow As DataRow ' create new row myrow = mydatatable.NewRow myrow("field_a") = "filed a row 1" myrow("field_b") = "filed b row 1" mydatatable.Rows.Add(myrow)

    GridView1.datasource = mydatatable
    gridview1.databind()

aspx code:

A: 

Maybe this article will solve part of the problem

http://www.codeproject.com/KB/webforms/GridViewColumns.aspx

Kronass