【问题标题】:How to display the specific values of a column in GridView Horizontally?如何在 GridView 中水平显示列的特定值?
【发布时间】: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 表格。使用&lt;%# Eval("Column")%&gt; 在此表中显示您的数据。
  • 我同意山姆的观点。在 ASPX 中使用 TemplateField。
  • 感谢您的回复。我会试试 TemplateField。您是否有任何对我的情况最有帮助的示例/链接?

标签: asp.net vb.net gridview


【解决方案1】:

而不是绑定字段使用 Templatefield 并在该字段中使用您喜欢的格式的 HTML 表格。使用&lt;%# Eval("Column")%&gt; 在此表中显示您的数据。

这是一个示例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;
        }
    }

【讨论】:

  • 如何将动态数据分配给 ?这可能吗?
  • 您无法从设计视图中执行此操作,但如果您从后面的代码动态创建 GridView 则可能。但是,这对你来说有点工作。如果你这样做,请使用 ItemCreated 事件。
  • 查看我对此答案的更新。我没有尝试此代码,但它应该可以工作。
  • 为 C# 代码道歉。我是一个 C# 人。但是,如果您不知道如何将此代码转换为 VB.Net,则可以在线使用 C# 到 VB.Net 转换器:-D
  • 日期 1-1-2014 和 1-2-2014 来自数据库,它不应该是硬编码的,我想让它看起来像这个先生。 prntscr.com/4re3on
【解决方案2】:

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

【讨论】:

  • 感谢您的快速响应,但不幸的是,我不应该编辑/移动查询,因为该存储过程在系统的多个部分中使用,我应该只修改后面的代码。
【解决方案3】:

另一种方法是将您的网格设置为 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;

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 2018-12-24
      • 2021-02-07
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 2018-02-25
      相关资源
      最近更新 更多