在aspx.vb中使用Gridview(代码隐藏)

时间:2022-04-30 23:54:34

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

我需要基于2个不同的数据源创建一个gridview:main和sub-cathegory。我需要列出如下:

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.

等等......问题是“productinfo”和“子产品”都是动态的,因为两者的数量可能不同,所以我必须在gridview中创建一个gridview,以及必要的过滤器。

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.

出于这个原因,我认为最好在代码隐藏中完成所有操作,但我无法理解如何在代码隐藏中使用gridview-class并将其绑定,以便它实际显示主aspx页面中的内容。

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

基本上我要求的是一个简单的例子,当你在aspx -page中只有 时,你可以从代码隐藏(vb)中添加组件并显示它吗?

Thanks.

4 个解决方案

#1


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:

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

#2


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:

我会在代码隐藏中正确创建gridview,如下所示:

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.

或者你需要在asp:GridView中添加一个Id和runat =“server”,以便能够在代码隐藏中使用它。

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

当您需要将组件放入gridview时,您需要绑定数据源,例如通用列表。

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?

如果你想要你的主要和子排列像你显示,我会使用2个网格?

#3


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.

首先,如果没有任何数据,ASP.NET根本不渲染gridview ......很烦人,但你能做些什么。 (实际上,你可能会做些什么,但我不知道它是什么!)。在数据表中添加一个新行,您将更近一步。

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.

其次,您要为数据表而不是gridview指定列,因此您希望AutoGenerateColumns =“True”,以便gridview在生成列时从表中选取列名。

#4


Faced with a similar problem, I found these articles very helpful:

面对类似的问题,我发现这些文章非常有用:

  1. GridView in ASP.NET is not displaying with or without data
  2. ASP.NET中的GridView不显示有或没有数据

  3. Create DataTable dynamically and bind to GridView in ASP.Net
  4. 动态创建DataTable并绑定到ASP.Net中的GridView

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
  • 在代码隐藏的Page_Load事件期间将GridView绑定到DataTable

  • check the IsPostBack property if you want to bind only on the first page load
  • 如果要仅在第一页加载时绑定,请检查IsPostBack属性

  • in the GridView properties, set AutoGenerateColumns="True"
  • 在GridView属性中,设置AutoGenerateColumns =“True”

#1


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:

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

#2


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:

我会在代码隐藏中正确创建gridview,如下所示:

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.

或者你需要在asp:GridView中添加一个Id和runat =“server”,以便能够在代码隐藏中使用它。

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

当您需要将组件放入gridview时,您需要绑定数据源,例如通用列表。

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?

如果你想要你的主要和子排列像你显示,我会使用2个网格?

#3


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.

首先,如果没有任何数据,ASP.NET根本不渲染gridview ......很烦人,但你能做些什么。 (实际上,你可能会做些什么,但我不知道它是什么!)。在数据表中添加一个新行,您将更近一步。

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.

其次,您要为数据表而不是gridview指定列,因此您希望AutoGenerateColumns =“True”,以便gridview在生成列时从表中选取列名。

#4


Faced with a similar problem, I found these articles very helpful:

面对类似的问题,我发现这些文章非常有用:

  1. GridView in ASP.NET is not displaying with or without data
  2. ASP.NET中的GridView不显示有或没有数据

  3. Create DataTable dynamically and bind to GridView in ASP.Net
  4. 动态创建DataTable并绑定到ASP.Net中的GridView

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
  • 在代码隐藏的Page_Load事件期间将GridView绑定到DataTable

  • check the IsPostBack property if you want to bind only on the first page load
  • 如果要仅在第一页加载时绑定,请检查IsPostBack属性

  • in the GridView properties, set AutoGenerateColumns="True"
  • 在GridView属性中,设置AutoGenerateColumns =“True”