如何在GridView中水平显示列的特定值?

时间:2014-09-29 01:59:18

标签: asp.net vb.net gridview

我无法在标题中进一步解释,所以在这里解释得更多。

这是我在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>

4 个答案:

答案 0 :(得分:1)

不是让绑定字段使用Templatefield,而是在该字段中使用您喜欢的格式的HTML表格。利用<%# Eval("Column")%>在此表格中显示您的数据。

以下是GridView的示例。 (注意:这只是一个例子可能无法正常工作。只是得到了想法)

<asp:GridView runat="server" ID="gvTest">
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <table>
                            <tr>
                                <td>Section</td>
                                <td>Exam</td>
                                <td>Normal Values</td>
                                <td>1-1-2014</td>
                                <td>1-2-2014</td>
                            </tr>                            
                    </HeaderTemplate>
                    <ItemTemplate>
                            <tr>
                                <td><%# Eval("Column1") %></td>
                                <%--Add other columns from your DataSet/DataTable match the columns--%>
                            </tr>                      
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

在这里阅读更多内容。

http://msdn.microsoft.com/en-us/library/aa479353.aspx http://msdn.microsoft.com/en-us/library/bb288032.aspx

您可以对Repeater

执行相同的操作

更新1

如何动态更改标题

试试这个

在标题中有一个Literal控件(此控件可以在渲染过程中吐出HTML)

<HeaderTemplate>
    <table>
       <asp:Literal ID="Literal1" runat="server"></asp:Literal>                                                       
</HeaderTemplate>

在您的代码中,在ItemCreated事件

中执行此类操作
  protected void gvTest_ItemCreated(Object sender, EventArgs e)
    {

        // Get the header row.
        GridViewRow headerRow = gvTest.HeaderRow;

        // Get the Literal control from the header row.
        Literal ltHeader = (Literal)headerRow.FindControl("Literal1");

        if (ltHeader != null)
        {
            // Build this headerRow string variable based on the values you want
            string headerRow = "<td>Section</td><td>Exam</td><td>Normal Values</td><td>1-1-2014</td><td>1-2-2014</td>";
            ltHeader.Text = headerRow;
        }
    }

答案 1 :(得分:0)

我认为,您可以在SQL Server中使用PIVOT实现这一目标。 PIVOT本质上将行值转换为列。请快速了解此http://www.codeproject.com/Tips/500811/Simple-Way-To-Use-Pivot-In-SQL-Query。通过这种方式,您可以放弃一些关于将行转换为列的UI代码。转换后的数据集将直接来自存储过程。

答案 2 :(得分:0)

另一种方法是将网格设置为autocolumn true选项,然后从后端(c#)创建一个数据集,将数据转换为网格数据源。最后将网格绑定到数据源。

请参阅以下代码: - Is it possible to switch rows and columns in a datagridview?

DataTable oldTable = new DataTable();

...

DataTable newTable = new DataTable();

newTable.Columns.Add("Field Name");
for (int i = 0; i < oldTable.Rows.Count; i++)
    newTable.Columns.Add();

for (int i = 0; i < oldTable.Columns.Count; i++)
{
    DataRow newRow = newTable.NewRow();

    newRow[0] = oldTable.Columns[i].Caption;
    for (int j = 0; j < oldTable.Rows.Count; j++)
        newRow[j+1] = oldTable.Rows[j][i];
    newTable.Rows.Add(newRow);
}

dataGridView.DataSource = newTable;

答案 3 :(得分:0)

这回答了我的问题。通过使用适当的循环和条件。 请参阅链接:Print Datagrid table with many column headers accross multiple pages in asp.net