asp.net 将表格中的数据合并到一个单元格中

6psbrbz9  于 2023-02-10  发布在  .NET
关注(0)|答案(1)|浏览(249)

我尝试从数据库加载一个表,我想将每列中的数据合并到单独的单元格中。这是下表:
| 色谱柱A|B栏|C栏|D栏|
| - ------|- ------|- ------|- ------|
| 单元格a1|单元格b1|单元格c1|单元格d1|
| 零|单元格b2|单元格c2|零|
| 零|零|单元格c3|零|
这是所需输出
| 色谱柱A|B栏|C栏|D栏|
| - ------|- ------|- ------|- ------|
| 单元格a1|单元格b1单元格b2|单元格c1单元格c2单元格c3|单元格d1|
这是我的代码加载表文件VB:

Private Sub LOAD_TABLE (ByVal iID As Integer)
    Dim cmd As New SqlCommand("LOAD_TB")
    Dim s As String = ""
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Parameters.Add("@ID", SqlDbType.Int).Value = iID
    cmd.Parameters.Add("@T", SqlDbType.Int).Value = DrListT.SelectedValue
    cmd.Parameters.Add("@N", SqlDbType.Int).Value = DrListN.SelectedValue

    Using con As New SqlConnection(Class_pub.conString)
        Using sda As New SqlDataAdapter()
            cmd.Connection = con
            sda.SelectCommand = cmd
            Using dt As New DataTable
                sda.Fill(dt)
                GRV_H.DataSource = dt
                GRV_H.DataBind()
            End Using
        End Using

        If con.State = ConnectionState.Open Then
            con.Close()
            con.Dispose()
        End If
    End Using
End Sub

以及在文件ASPX中显示表的代码:

<asp:GridView ID="GRV_H" DataKeyNames="ID" AutoGenerateColumns="False" runat="server"
    AllowPaging="False" Width="100%">
    <AlternatingRowStyle BackColor="#FCFCFC" />
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <input id="id1" type="text" name="txt1" style="width: 80px; font-family: Arial"
                    class="textbox_numeric" value='<%# Eval("ColummA", "{0:#,##0}") %>' onkeyup="this.value=FormatNumber(this.value);"
                     onclick="return this.focus();" onfocus="this.select();"
                    autocomplete="Off" runat="server" />
            </ItemTemplate>
            <HeaderTemplate>
                Columm A</HeaderTemplate>
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="60px" />
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <input id="id2" type="text" name="txt2" style="width: 80px; font-family: Arial"
                    class="textbox_numeric" value='<%# Eval("ColummB", "{0:#,##0}") %>' onkeyup="this.value=FormatNumber(this.value);"
                     onclick="return this.focus();" onfocus="this.select();"
                    autocomplete="Off" runat="server" />
            </ItemTemplate>
            <HeaderTemplate>
                Column B</HeaderTemplate>
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="60px" />
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <input id="id3" type="text" name="txt3" style="width: 75px; font-family: Arial"
                    class="textbox_numeric" value='<%# Eval("ColummC", "{0:#,##0}") %>' readonly="readonly" onclick="return this.focus();" onfocus="this.select();"
                    autocomplete="Off" runat="server" />
            </ItemTemplate>
            <HeaderTemplate>
                Columm C</HeaderTemplate>
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="75px" />
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <input id="id4" type="text" name="txt4" style="width: 60px; font-family: Arial"
                    class="textbox_numeric" value='<%# Eval("ColummD", "{0:#,##0}") %>' onkeyup="this.value=FormatNumber(this.value);"
                    onchange="" onclick="return this.focus();" onfocus="this.select();" autocomplete="Off"
                    runat="server" />
            </ItemTemplate>
            <HeaderTemplate>
                Columm D</HeaderTemplate>
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="60px" />
        </asp:TemplateField>                             
    </Columns>
    <HeaderStyle CssClass="style_header_Grid" />
    <RowStyle CssClass="style_item_Grid" />
    <PagerStyle CssClass="style_header_Grid" Font-Bold="true" />
</asp:GridView>

我没有找到解决方案,因为我的英语不好,调用或描述问题。

fkaflof6

fkaflof61#

好吧,"接近"这个逻辑和问题的方法是什么?
在代码中处理表级别的数据,而不是从网格视图(用于显示数据)处理数据。
所以逻辑:
将数据加载到表中。
创建第二个表,并将上述行处理为第二个表中的一行。
将第二个表发送到GridView。
假设这个标记

<asp:Button ID="Button1" runat="server" Text="Load Grid" CssClass="btn"
    OnClick="Button1_Click"
    />
<br />
<br />

<h3>Original table data</h3>
<asp:GridView ID="GridView1" runat="server" CssClass="table"
    Width="30%" AutoGenerateColumns="false">
    <Columns>
            <asp:BoundField DataField="LastName"/>
            <asp:BoundField DataField="City"/>
            <asp:BoundField DataField="HotelName"/>
    </Columns>
</asp:GridView>

<br />
<h3>Crunched table data</h3>

<asp:GridView ID="GridView2" runat="server" CssClass="table"
    Width="30%" AutoGenerateColumns="false">
    <Columns>
            <asp:BoundField DataField="LastName" HtmlEncode="False" />
            <asp:BoundField DataField="City" HtmlEncode="False" />
            <asp:BoundField DataField="HotelName" HtmlEncode="False" />
    </Columns>
</asp:GridView>

要加载和处理的代码如下所示:

Protected Sub Button1_Click(sender As Object, e As EventArgs)

    Dim strSQL As String =
        "SELECT LastName, City, HotelName FROM tblHotels
        WHERE ID IN (127, 120, 124)"

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

            Dim rstSource As New DataTable
            Dim rstCrunched As New DataTable

            conn.Open()

            rstSource.Load(cmdSQL.ExecuteReader)

            GridView1.DataSource = rstSource    ' show origional data
            GridView1.DataBind()

            For Each MyColumn In rstSource.Columns
                rstCrunched.Columns.Add(MyColumn.ColumnName)
            Next

            Dim ResultRow As DataRow = rstCrunched.NewRow
            For Each OneRow As DataRow In rstSource.Rows
                For Each MyColumn As DataColumn In rstSource.Columns
                    If ResultRow(MyColumn.ColumnName).ToString <> "" Then
                        ResultRow(MyColumn.ColumnName) &= "<br/>"
                    End If
                    ResultRow(MyColumn.ColumnName) &= OneRow(MyColumn.ColumnName)
                Next

            Next
            rstCrunched.Rows.Add(ResultRow)
            GridView2.DataSource = rstCrunched    ' show crunched data
            GridView2.DataBind()

        End Using
    End Using
End Sub

点击按钮时的结果是

然而,这里有很多问题。
如果使用绑定字段,则需要HtmlEncode ="False"以上。
然而,如果你使用标签,(在模板列中),那么再一次使用上面方法就可以了。
如果您使用文本框,那么这:

ResultRow(MyColumn.ColumnName) &= vbCrLf

因此,无论哪种方式,数据的预处理都是一条值得走的路。

相关问题