2016-04-15 112 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; 
      } 
     } 
    } 
}