【问题标题】:How to merge rows in gridview如何在gridview中合并行
【发布时间】:2016-04-15 08:48:54
【问题描述】:

我在gridview中有一些数据如下。

我想合并gridview中的行如下。

我已经在 RowDataBound() 和 PreRender() 中尝试了以下代码,结果与我想要的不一样。

for (int rowIndex = GridView1.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
    GridViewRow gvRow = GridView1.Rows[rowIndex];
    GridViewRow gvPreviousRow = GridView1.Rows[rowIndex + 1];
    for (int cellCount = 0; cellCount < gvRow.Cells.Count; cellCount++)
    {
        if (gvRow.Cells[cellCount].Text ==
                            gvPreviousRow.Cells[cellCount].Text)
        {
            if (gvPreviousRow.Cells[cellCount].RowSpan < 2)
            {
                gvRow.Cells[cellCount].RowSpan = 2;
            }
            else
            {
                gvRow.Cells[cellCount].RowSpan =
                    gvPreviousRow.Cells[cellCount].RowSpan + 1;
            }
            gvPreviousRow.Cells[cellCount].Visible = false;
        }
    }
}

在 aspx 中,

<asp:GridView ID="GridView1" runat="server" Width="100%"
    AutoGenerateColumns = "false" AlternatingRowStyle-BackColor = "#fffccc" HeaderStyle-ForeColor ="#ffffff" 
    HeaderStyle-BackColor = "#333" AllowPaging ="true" OnRowDataBound="GridView1_RowDataBound" PageSize = "20"
    OnPageIndexChanging="GridView1_PageIndexChanging" OnPreRender="GridView1_PreRender">
    <HeaderStyle Height="30px" />
        <Columns>
            <asp:TemplateField HeaderText="Client Company">
                <ItemTemplate>
                    <asp:Label ID="lblClientCompany" runat="server" Text='<%# Eval("ClientCompany")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Position">
                <ItemTemplate>
                    <asp:Label ID="lblPosition" runat="server" Text='<%# Eval("Position")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Candidate">
                <ItemTemplate>
                    <asp:Label ID="lblCandidate" runat="server" Text='<%# Eval("Candidate")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Status">
                <ItemTemplate>
                    asp:Label ID="lblStatus" runat="server" Text='<%# Eval("Status")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    <AlternatingRowStyle BackColor="#fffccc"  />
</asp:GridView>

请帮帮我,因为我不知道。谢谢。

【问题讨论】:

  • 运行代码的结果是什么?
  • 运行代码时只显示第一行。
  • @SandarMinAye 发布您的 aspx gridview 代码
  • @VigneshKumar,我已经在我的帖子中放了 gridview 代码。
  • @SandarMinAye 我已经添加了我的答案。请检查并告诉我

标签: c# asp.net gridview


【解决方案1】:

你需要尝试DataBound而不是RowDataBound中的方法

DataBound 在 GridView 控件绑定到数据源后触发(在绑定所有行之后)。

当数据行绑定到 GridView 控件中的数据时,每行都会触发RowDataBound

ASPX

<asp:GridView ID="GridView1" runat="server" Width="100%"
    AutoGenerateColumns = "false" AlternatingRowStyle-BackColor = "#fffccc" HeaderStyle-ForeColor ="#ffffff" 
    HeaderStyle-BackColor = "#333" AllowPaging ="true"  OnDataBound="GridView1_DataBound1" PageSize = "20"
    OnPageIndexChanging="GridView1_PageIndexChanging">
    <HeaderStyle Height="30px" />
        <Columns>
            <asp:TemplateField HeaderText="Client Company">
                <ItemTemplate>
                    <asp:Label ID="lblClientCompany" runat="server" Text='<%# Eval("ClientCompany")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Position">
                <ItemTemplate>
                    <asp:Label ID="lblPosition" runat="server" Text='<%# Eval("Position")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Candidate">
                <ItemTemplate>
                    <asp:Label ID="lblCandidate" runat="server" Text='<%# Eval("Candidate")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Status">
                <ItemTemplate>
                    asp:Label ID="lblStatus" runat="server" Text='<%# Eval("Status")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    <AlternatingRowStyle BackColor="#fffccc"  />
</asp:GridView>

代码背后

protected void GridView1_DataBound1(object sender, System.EventArgs e)
{
    for (int rowIndex = GridView1.Rows.Count - 2; rowIndex >= 0; rowIndex--)
    {
        GridViewRow gvRow = GridView1.Rows[rowIndex];
        GridViewRow gvPreviousRow = GridView1.Rows[rowIndex + 1];
        for (int cellCount = 0; cellCount < gvRow.Cells.Count; cellCount++)
        {
            if (gvRow.Cells[cellCount].Text ==
                                gvPreviousRow.Cells[cellCount].Text)
            {
                if (gvPreviousRow.Cells[cellCount].RowSpan < 2)
                {
                    gvRow.Cells[cellCount].RowSpan = 2;
                }
                else
                {
                    gvRow.Cells[cellCount].RowSpan =
                        gvPreviousRow.Cells[cellCount].RowSpan + 1;
                }
                gvPreviousRow.Cells[cellCount].Visible = false;
            }
        }
    }
}

【讨论】:

  • 你改变了数据绑定吗?
【解决方案2】:

正如 Vignesh Kumar 所建议的,处理可以在 DataBound 事件中完成,在所有行都被填充之后。

为了检索字段值,我建议将它们添加到 GridView 的 DataKeyNames 属性中:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="ClientCompany,Position" OnDataBound="GridView1_DataBound" ... />

下面是合并单元格的代码:

protected void GridView1_DataBound(object sender, EventArgs e)
{
    for (int currentRowIndex = 0; currentRowIndex < GridView1.Rows.Count; currentRowIndex++)
    {
        GridViewRow currentRow = GridView1.Rows[currentRowIndex];
        CombineColumnCells(currentRow, 0, "ClientCompany");
        CombineColumnCells(currentRow, 1, "Position");
    }
}

private void CombineColumnCells(GridViewRow currentRow, int colIndex, string fieldName)
{
    TableCell currentCell = currentRow.Cells[colIndex];

    if (currentCell.Visible)
    {
        Object currentValue = GridView1.DataKeys[currentRow.RowIndex].Values[fieldName];

        for (int nextRowIndex = currentRow.RowIndex + 1; nextRowIndex < GridView1.Rows.Count; nextRowIndex++)
        {
            Object nextValue = GridView1.DataKeys[nextRowIndex].Values[fieldName];

            if (nextValue.ToString() == currentValue.ToString())
            {
                GridViewRow nextRow = GridView1.Rows[nextRowIndex];
                TableCell nextCell = nextRow.Cells[colIndex];
                currentCell.RowSpan = Math.Max(1, currentCell.RowSpan) + 1;
                nextCell.Visible = false;
            }
            else
            {
                break;
            }
        }
    }
}

【讨论】:

    【解决方案3】:

    我在 vb.net 后面的代码中为第一列创建了:

    注意:列应该是BoundField。

    <asp:BoundField DataField="ProductID" HeaderText="ProductID">
          <ItemStyle Width="70px" HorizontalAlign="Center" />
    </asp:BoundField>
    

    背后的VB.Net代码:

    Private Sub gvGridView_DataBound(sender As Object, e As EventArgs) Handles gvGridView.DataBound
    
        Dim rowCount As Integer = gvTranscript1.Rows.Count
        Dim RowIndex As Integer = 0
        Dim gvRow As GridViewRow
        Dim gvPrevRow As GridViewRow
        Dim cellIndex As Integer = 0
    
        For RowIndex = rowCount - 2 To 0 Step -1
            gvRow = gvTranscript1.Rows(RowIndex) 'second last
            gvPrevRow = gvTranscript1.Rows(RowIndex + 1) 'last
            'lblmsg.Text += gvRow.Cells(0).Text & " - " & gvPrevRow.Cells(0).Text & "<br>"
            If gvRow.Cells(0).Text = gvPrevRow.Cells(0).Text Then
                If gvPrevRow.Cells(cellIndex).RowSpan < 2 Then
    
                    gvRow.Cells(cellIndex).RowSpan = 2
    
                Else
    
                    gvRow.Cells(cellIndex).RowSpan = gvPrevRow.Cells(cellIndex).RowSpan + 1
    
                End If
                gvPrevRow.Cells(0).Visible = False
            End If
        Next
    
    End Sub
    

    【讨论】:

      【解决方案4】:
      Dim rows = GridView1.Rows.Cast(Of GridViewRow).Union(GridView2.Rows.Cast(Of GridViewRow)).ToArray().Select(Function(f) New With {.YourColumn1 = f.Cells(0).Text, .YourColumn2 = f.Cells(1).Text, .YourColumnN = f.Cells(n).Text})
      GridView1.DataSource = rows
      GridView1.DataBind()
      

      【讨论】:

        【解决方案5】:

        移除 ItemTemplate 并获取 boundField 列

        【讨论】:

          猜你喜欢
          • 2012-05-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-29
          • 2014-11-22
          • 1970-01-01
          相关资源
          最近更新 更多