在asp.net中以水平和垂直方式显示数据

时间:2011-06-08 14:54:00

标签: asp.net sql-server-2005

我在显示数据库中的数据时遇到问题。我的表中有6列(EmpName,EmpID,AdminCode,Practice,Hours,FTE),我想显示如下数据

blankArea       blankArea   blankarea        P1(PracticeName)    P2              P3
EmpName         EmpID      AdminCode     Hours   FTE        Hours   FTE     Hours    FTE

A 1 Insurance 0.14 0.03 0.00 0.00 0.00 0.00 A 1 AllDocs 0.19 0.04 0.00 0.00 0.00 0.00 B 2 Insurance 0.52 0.11 1.18 0.25 0.00 0.00 B 2 Payments 1.18 0.35 0.00 0.00 0.00 0.00 C 3 Payments 1.31 0.00 0.00 0.00 0.00 0.00

这意味着我的表中的所有细节练习名也在表中,我使用sql server 2005.如何以上述格式显示我的数据。实践名称水平显示,自动生成两列,并使用数据库中存在的小时数和FTE列数据填充这些列。 转发器或数据列表是否适用于此。如果是,那么如何。你有任何代码吗?请帮忙。

提前致谢

2 个答案:

答案 0 :(得分:0)

在我脑海中浮现的第一件事就是:

  • 一个全局Gridview:EmpName,EmpID,AdminCode,Practices
  • 在全局网格视图的“实践”项目模板中显示一个转发器。
  • 另一个有一个练习细节的网格视图,这个网格视图将在转发器中,因此每次练习都会重复。

这是我应该做的,也许有一种更容易的方法不确定。 希望这会让你走上正轨。

答案 1 :(得分:0)

我会使用GridViewRowCreated事件来生成标题中的额外行:

ASPX:

<asp:GridView ID="GridView1" runat="server" OnRowCreated="GridView1_RowCreated" ></asp:GridView>

代码隐藏(VB.Net):

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        'fake data
        Dim tbl As New DataTable
        tbl.Columns.Add(New DataColumn("EmpName", GetType(String)))
        tbl.Columns.Add(New DataColumn("EmpID", GetType(String)))
        tbl.Columns.Add(New DataColumn("AdminCode", GetType(String)))
        tbl.Columns.Add(New DataColumn("P1Hours", GetType(String)))
        tbl.Columns.Add(New DataColumn("P1FTE", GetType(String)))
        tbl.Columns.Add(New DataColumn("P2Hours", GetType(String)))
        tbl.Columns.Add(New DataColumn("P2FTE", GetType(String)))
        tbl.Columns.Add(New DataColumn("P3Hours", GetType(String)))
        tbl.Columns.Add(New DataColumn("P3FTE", GetType(String)))
        For i As Int32 = 1 To 100
            Dim newRow = tbl.NewRow
            newRow("EmpName") = "EmpName" & i
            newRow("EmpID") = "EmpID" & i
            newRow("AdminCode") = "AdminCode" & i
            newRow("P1Hours") = "P1Hours" & i
            newRow("P1FTE") = "P1FTE" & i
            newRow("P2Hours") = "P2Hours" & i
            newRow("P2FTE") = "P2FTE" & i
            newRow("P3Hours") = "P3Hours" & i
            newRow("P3FTE") = "P3FTE" & i
            tbl.Rows.Add(newRow)
        Next
        Me.GridView1.DataSource = tbl
        Me.GridView1.DataBind()
    End If
End Sub

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.Header Then
        'Build own custom header.
        Dim grid As GridView = DirectCast(sender, GridView)
        Dim newHeaderRow As New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert)
        Dim oTableCell As New TableCell()

        'Add empty cell
        newHeaderRow.Cells.Add(oTableCell)

        'Add empty cell
        oTableCell = New TableCell()
        newHeaderRow.Cells.Add(oTableCell)

        'Add empty cell
        oTableCell = New TableCell()
        newHeaderRow.Cells.Add(oTableCell)

        'Add P1(PracticeName)-Cell
        oTableCell = New TableCell()
        oTableCell.Text = "P1(PracticeName)"
        oTableCell.ColumnSpan = 2
        oTableCell.Font.Bold = True
        oTableCell.HorizontalAlign = HorizontalAlign.Center
        newHeaderRow.Cells.Add(oTableCell)

        'Add P2-Cell
        oTableCell = New TableCell()
        oTableCell.Text = "P2"
        oTableCell.ColumnSpan = 2
        oTableCell.Font.Bold = True
        oTableCell.HorizontalAlign = HorizontalAlign.Center
        newHeaderRow.Cells.Add(oTableCell)

        'Add P3-Cell
        oTableCell = New TableCell()
        oTableCell.Text = "P3"
        oTableCell.ColumnSpan = 2
        oTableCell.Font.Bold = True
        oTableCell.HorizontalAlign = HorizontalAlign.Center
        newHeaderRow.Cells.Add(oTableCell)
        grid.Controls(0).Controls.AddAt(0, newHeaderRow)
    End If
End Sub

如果您需要C#,可以将其转换为here

结果如下:

image of the resulting GridView