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:

   sub-product 1
   sub-product 2

   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)?



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"

GridView1.datasource = mydatatable

aspx code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" EmptyDataText="if im not at my desk im at the balcony contemplating suicide" >

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

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:

  1. GridView in ASP.NET is not displaying with or without data
  2. 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"

Need Your Help

Change position of Google Maps API's “My location” button

java android google-maps-android-api-2 android-maps-v2

I am using the Google Maps Android API v2, and I need a way to chance the position of the "My Location" button.

python django - no module psycopg2.extension even after installing compiled version psycopg2-2.4.5.win32-py2.7.‌exe

python django heroku psycopg2

I am using python django on windows,and trying to deploy to heroku . When i tried to install psycopg2 using pip,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.