I am dynamically generating HtmlGeneric Controls with data in a DataTable. I need to concatenate HTML to the beginning and end of each group of 3. For example, my table looks something like this:
Title Body
------------------------------
Order 1 This is body 1
Order 2 This is body 2
Order 3 This is body 3
Order 4 This is body 4
Order 5 This is body 5
Order 6 This is body 6
I'm currently generating HTML like this with no issue:
<div class="card">
<div class="card-body">
<h5 class="card-title">Order 1</h5>
<p class="card-text">This is body 1</p>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Order 2</h5>
<p class="card-text">This is body 2</p>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Order 3</h5>
<p class="card-text">This is body 3</p>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Order 4</h5>
<p class="card-text">This is body 4</p>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Order 5</h5>
<p class="card-text">This is body 5</p>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Order 6</h5>
<p class="card-text">This is body 6</p>
</div>
</div>
I need to combine a foreach loop and a counter to concatenate a div with a card-group class at the beginning and end of every 3 cards like this. Of course, in reality there are a lot more rows than 6.:
<div class="card-group">
<div class="card">
<div class="card-body">
<h5 class="card-title">Order 1</h5>
<p class="card-text">This is body 1</p>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Order 2</h5>
<p class="card-text">This is body 2</p>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Order 3</h5>
<p class="card-text">This is body 3</p>
</div>
</div>
</div>
<div class="card-group">
<div class="card">
<div class="card-body">
<h5 class="card-title">Order 4</h5>
<p class="card-text">This is body 4</p>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Order 5</h5>
<p class="card-text">This is body 5</p>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title">Order 6</h5>
<p class="card-text">This is body 6</p>
</div>
</div>
</div>
It's very simple without the need for concatenating the additional HTML. This is executed in my page load method:
DataTable dtCards = GetData();
foreach (DataRow row in dtCards.Rows)
{
var titleText = row["Title"].ToString();
var bodyText = row["Body"].ToString();
CreateDiv(titleText, bodyText); //method to create HmtlGeneric Controls
}
I'm assuming I'll need to combine a foreach loop with a counter inside the CreateDiv method but cannot wrap my mind around how exactly that will work out. Especially if there's an odd number of rows - the card-group div would always need a closing tag on the final row.
Shane, what you are essentially doing is Paging through your Rows in your table, so focus on Skip and Take Linq extension methods. Below is a hasty sample
you get something like this.