2016-04-15 71 views
1

我在gridview中有一些数据,如下所示。如何在gridview中合并行

enter image description here

我想在GridView的合并,如下行。

enter image description here

我已经用的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> 

请帮我becasue我不知道。谢谢。

+0

请告诉我结果的时候你运行你的代码? –

+0

当我运行我的代码时,只显示第一行。 –

+0

@SandarMinAye发表你的aspx gridview代码 –

回答

2

你需要尝试的方法DataBoundRowDataBound

DataBound火灾GridView控件绑定到数据源后(所有行绑定后)。

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

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; 
      } 
     } 
    } 
} 
+0

,只显示第一行。 –

+0

是否改变了数据库? –

+0

是的,我改变了你的表现。 –

0

至于建议的维涅什库马尔,该处理可在DataBound事件中完成,所有的行已经被填充后。

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

​​

这里是细胞结合代码:

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; 
      } 
     } 
    } 
}