Using Gridview in aspx.vb (code-behind)
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)?
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()
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" EmptyDataText="if im not at my desk im at the balcony contemplating suicide" > <Columns></Columns> </asp:GridView>
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?
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.
Faced with a similar problem, I found these articles very helpful:
- GridView in ASP.NET is not displaying with or without data
- Create DataTable dynamically and bind to GridView in ASP.Net
Based on what I learned, the following steps worked for me:
- bind your GridView to the DataTable during the Page_Load event of your code-behind
- check the IsPostBack property if you want to bind only on the first page load
- in the GridView properties, set AutoGenerateColumns="True"