2016-04-15 4 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> 

Пожалуйста, помогите мне еще потому я понятия не имею. Благодарю.

+0

Что результат, когда вы запустить свой код? –

+0

только первая строка отображается при запуске моего кода. –

+0

@SandarMinAye Опубликовать свой код сетки aspx –

ответ

2

Вы должны попробовать метод в DataBound не RowDataBound

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

сделал u изменение ondatabound? –

+0

Да, я изменился, как вы показали. –

0

Как полагает Вигнеш Кумар, обработка может быть сделано в DataBound случае, после того, как все строки были заполнены.

Для того, чтобы восстановить значения поля, я предлагаю добавить их к атрибуту DataKeyNames в GridView:

<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; 
      } 
     } 
    } 
} 
Смежные вопросы