【发布时间】:2014-11-23 08:27:42
【问题描述】:
我无法在标题中进一步解释,所以在这里再解释一下。
这是我在 gridview 中的当前界面:
---------------------------------------------------------------------------
Section | Exam | Normal Values | Result | Result Date |
---------------------------------------------------------------------------
| Calcium | NormalValue1 | Result1 | 1-1-2014 |
Chemistry |--------------------------------------------------------------|
| Sodium | NormalValue2 | Result2 | 1-2-2014 |
---------------------------------------------------------------------------
我需要让它看起来像这样:
---------------------------------------------------------------------------
Section | Exam | Normal Values | 1-1-2014 | 1-2-2014 |
--------------------------------------------------------------------------|
| Calcium | NormalValue1 | Result1 | |
Chemistry |--------------------------------------------------------------|
| Sodium | NormalValue2 | | Result2 |
--------------------------------------------------------------------------|
这是一个打印屏幕以便更好地查看:http://prntscr.com/4re3on
我需要水平显示日期并在其下方显示结果。我通过存储过程获取数据。我尝试将 GridView 旋转成列,但它看起来不正确。我该怎么做?
这是我的代码:
Private Sub LoadGrid()
Dim o_Dataset As New DataSet()
Using sqlConn As New SqlConnection(DataSource.ConnectionString)
Using sqlCmd As New SqlCommand()
Dim sqlAdapter As New SqlDataAdapter(sqlCmd)
sqlCmd.CommandText = "Station.dbo.[sp_Nurse_GetPatient_LabResult_NormalValues_Tabular_New]"
sqlCmd.CommandType = CommandType.StoredProcedure
'sqlCmd.Parameters.Add(New SqlParameter("@labsectionid", "H"))
sqlCmd.Parameters.Add(New SqlParameter("@HospNum", Session.Item("HospNum")))
sqlCmd.Connection = sqlConn
sqlConn.Open()
Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader()
sqlReader.Close()
sqlAdapter.Fill(o_Dataset)
grdReports_H.DataSource = o_Dataset.Tables(0)
grdReports_H.DataBind()
GroupGridView(grdReports_H.Rows, 0, 3)
sqlConn.Close()
End Using
End Using
End Sub
这段代码只是隐藏了我不需要的数据
Protected Sub OnRowDataBound_H(sender As Object, a As GridViewRowEventArgs)
If a.Row.Cells(0).Text = "A" Then
a.Row.Visible = False
End If
End Sub
这段代码只是对我的数据进行分组,就像在(化学)部分显示的那样:
Private Sub GroupGridView(gvrc As GridViewRowCollection, startIndex As Integer, total As Integer)
If total = 0 Then
Return
End If
Dim i As Integer, count As Integer = 1
Dim lst As New ArrayList()
lst.Add(gvrc(0))
Dim ctrl = gvrc(0).Cells(startIndex)
For i = 1 To gvrc.Count - 1
Dim nextCell As TableCell = gvrc(i).Cells(startIndex)
If ctrl.Text = nextCell.Text Then
count += 1
nextCell.Visible = False
lst.Add(gvrc(i))
Else
If count > 1 Then
ctrl.RowSpan = count
GroupGridView(New GridViewRowCollection(lst), startIndex + 1, total - 1)
End If
count = 1
lst.Clear()
ctrl = gvrc(i).Cells(startIndex)
lst.Add(gvrc(i))
End If
Next
If count > 1 Then
ctrl.RowSpan = count
GroupGridView(New GridViewRowCollection(lst), startIndex + 1, total - 1)
End If
count = 1
lst.Clear()
End Sub
这是我的 aspx 文件:
<style type="text/css">
.hiddencol
{
display: none;
}
</style>
<asp:GridView OnRowDataBound = "OnRowDataBound_H" ID="grdReports_H" AutoGenerateColumns="False" runat="server" CellPadding="4" EnableModelValidation="True" ForeColor="#333333" style="text-align: center">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="labsectionid" HeaderText="SectionID_H" ItemStyle-Width ="200px" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" />
<asp:BoundField DataField="Section" ItemStyle-Height="10px" HeaderText="Section" ItemStyle-Width="40px" ItemStyle-HorizontalAlign="right" ItemStyle-VerticalAlign="Top" />
<asp:BoundField DataField="Exam" HeaderText="Exam" ItemStyle-Width="150px" />
<asp:BoundField DataField="NormalValue" HeaderText="Normal Values" ItemStyle-Width="150px" />
<asp:BoundField DataField="Result" HeaderText="Result" ItemStyle-Width="150px" />
<asp:BoundField DataField="ResultDate" HeaderText="Result Date" ItemStyle-Width="150px" />
</Columns>
【问题讨论】:
-
我认为操作 DataTable 中的数据比弄乱 GridView 更容易。
-
而不是绑定字段使用 Templatefield 并在该字段中使用您喜欢的格式的 HTML 表格。使用
<%# Eval("Column")%>在此表中显示您的数据。 -
我同意山姆的观点。在 ASPX 中使用 TemplateField。
-
感谢您的回复。我会试试 TemplateField。您是否有任何对我的情况最有帮助的示例/链接?