Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
Rowspan Table rows in C# HTML with 'For loop' based on certain value

I would like to do a Rowspan the first column of my table.

My Current table looks like this:

Type Color Num
Car Red 1
Car Black 2
Car Black 3
Bike Yellow 4
Bike Green 5
Bike Yellow 6

What I want is to merge all the rows that have the same TYPE. For this example, I should get in the Type Column only two rows; Car and Bike. I know how to do the collapse, but the thing is that I'm getting my table from data base like this :

<table class="table table-striped table-bordered " style="display:inline-table;"> 
<thead>
   <tr >     
        <th class="col-1">
                    Type
        </th> 
        <th class="col-2">
                    Color
        </th>
        <th class="col-2">
                  Num  
      </th>
    </tr>
</thead>
<tbody>

    @foreach (var item in Model)
    {
         <tr>
     
        <td>
          
                  @item.Type
        </td> 
        <td>
          
                  @item.Color
        </td>
        <td>
                  @item.Num
        </td>     
  
    </tr>
 
    }
</tbody>

    </table> 

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Group your data:

<tbody>
@foreach (var group in Model.GroupBy(i => i.Type))
{
    var items = group.ToList();
    <tr>
        <td rowspan="@items.Count">@group.Key</td>
        <td>@items[0].Color</td>
        <td>@items[0].Num</td>
    </tr>
    @for (int index = 1; index < items.Count; index++)
    {
        <tr>
            <td>@items[index].Color</td>
            <td>@items[index].Num</td>
        </tr>
    }
}
</tbody>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda