【问题标题】:ASP.NET repeater how to get two columns (header and items) to verticalASP.NET 中继器如何使两列(标题和项目)垂直
【发布时间】:2021-10-01 01:46:25
【问题描述】:

你能帮帮我吗?

我的数据库中有一个这样的表:

January February
100.000 200.000

我想使用转发器显示这样的数据:

January 100.000
February 200.000

请帮帮我,这样我的 ASP.NET 前端的显示就会像这样;我使用 VB.NET。

【问题讨论】:

  • 出于好奇,您为什么在 2021 年使用 WebForms?它已经过时了 13 年了……

标签: asp.net vb.net webforms asprepeater datarepeater


【解决方案1】:

好的,既然我们在谈论数据表?

那么使用中继器确实不是正确类型的“重复”控件。

因此,每个数据行的重复器对于这种类型的重复来说是好的:

或者这样说:

换句话说,它不适用于像布局这样的表格。

对于像布局这样的表格,我建议使用 gridview - 为此目的更好。

所以,我们可以放入一个网格视图——我使用向导来创建它——然后从页面中删除数据源。无论如何,我们现在有了这个:

    <div style="width:20%;margin-left:20px;margin-top:20px">
        
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="table table-hover">
            <Columns>
                <asp:BoundField DataField="MyMonth" HeaderText="MyMonth"  />
                <asp:BoundField DataField="Sales" HeaderText="Sales" DataFormatString="{0:C}"/>
            </Columns>

        </asp:GridView>

    </div>

所以,现在我们需要对非常糟糕的数据进行反规范化。该数据看起来像电子表格 - 对于“数据”或“数据驱动的方法”来说并不是很好。

所以,让我们在后面编写这段代码来加载数据。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        LoadGrid
    End If

End Sub


Sub LoadGrid()

    Dim strSQL As String
    strSQL = "SELECT 1 as NMonth, 'January'  as MyMonth, January  As Sales from tblMonth " &
       "UNION SELECT 2 as NMonth, 'Feburary' as MyMonth, Feburary As Sales from tblmonth " &
       "UNION SELECT 3 as Nmonth, 'March'    as MyMonth, March    As Sales from tblmonth " &
       "UNION SELECT 4 as Nmonth, 'April'    as MyMonth, April    As Sales from tblmonth " &
       "ORDER BY Nmonth"

    Using cmdSQL As New SqlCommand(strSQL, New SqlConnection(My.Settings.TEST4))

        cmdSQL.Connection.Open()

        Dim rstData As New DataTable
        rstData.Load(cmdSQL.ExecuteReader)
        GridView1.DataSource = rstData
        GridView1.DataBind()

    End Using

End Sub

现在我们的输出如下所示:

我们的数据表如下所示:

因此,该数据库需要一些爱和关怀 - 它不应该以这种方式设置(每列以一个月为单位。数据库应该是:

  DMonth - (the month - probably better as a date
  Sales  - (the sales for the month - say money data type)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 2017-10-09
    • 2017-11-22
    • 1970-01-01
    相关资源
    最近更新 更多