Hi ramco1917,
As this is not possible through Repeater.Therefore we take GridView.
Please refer below sample.
HTML
<asp:GridView runat="server" ID="gvDetails" />
<asp:Button runat="server" Text="Convert" OnClick="Convert" />
Namespace
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt1 = new DataTable();
dt1.Columns.AddRange(new DataColumn[] { new DataColumn("ID"),
new DataColumn("Name"),
new DataColumn("TrainingDateFrom"),
new DataColumn("TrainingDateTo")});
dt1.Rows.Add("1", "Dealer", "20/09/2022", "25/09/2022");
DataTable dt2 = new DataTable();
dt2.Columns.AddRange(new DataColumn[] {
new DataColumn("Aid"),
new DataColumn("ID"),
new DataColumn("Date"),
new DataColumn("Status")});
dt2.Rows.Add("1", "1", "20/09/2022", "P");
dt2.Rows.Add("2", "1", "21/09/2022", "P");
dt2.Rows.Add("3", "1", "22/09/2022", "A");
dt2.Rows.Add("4", "1", "23/09/2022", "P");
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Name"),
new DataColumn("Date"),
new DataColumn("Status")});
var result = (from table1 in dt1.AsEnumerable()
join table2 in dt2.AsEnumerable()
on table1["ID"] equals table2["ID"]
select new
{
Name = table1["Name"],
Date = table2["Date"],
Status = table2["Status"]
});
foreach (var item in result.ToList())
{
dt.Rows.Add(item.Name, item.Date, item.Status);
}
ViewState["dt"] = dt;
BindGrid(dt);
}
}
protected void Convert(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
DataTable dt2 = new DataTable();
dt2.Columns.Add(new DataColumn("Id"));
dt2.Columns.Add("Name");
List<string> dates = dt.AsEnumerable().Select(x => x["Date"].ToString()).Distinct().ToList();
for (int i = 0; i < dates.Count; i++)
{
dt2.Columns.Add(dates[i]);
}
List<string> names = dt.AsEnumerable().Select(x => x["Name"].ToString()).Distinct().ToList();
for (int i = 0; i < names.Count; i++)
{
DataRow dr = dt2.NewRow();
dr[0] = i + 1;
dr[1] = names[i];
for (int j = 0; j < dates.Count; j++)
{
DataRow[] row = dt.Select("Name='" + names[i] + "' AND Date='" + dates[j] + "'");
dr[j + 2] = row.Length > 0 ? row[0]["Status"].ToString() : string.Empty;
}
dt2.Rows.Add(dr);
}
BindGrid(dt2);
}
private void BindGrid(DataTable dt)
{
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt1 As DataTable = New DataTable()
dt1.Columns.AddRange(New DataColumn() {New DataColumn("ID"), New DataColumn("Name"), New DataColumn("TrainingDateFrom"), New DataColumn("TrainingDateTo")})
dt1.Rows.Add("1", "Dealer", "20/09/2022", "25/09/2022")
Dim dt2 As DataTable = New DataTable()
dt2.Columns.AddRange(New DataColumn() {New DataColumn("Aid"), New DataColumn("ID"), New DataColumn("Date"), New DataColumn("Status")})
dt2.Rows.Add("1", "1", "20/09/2022", "P")
dt2.Rows.Add("2", "1", "21/09/2022", "P")
dt2.Rows.Add("3", "1", "22/09/2022", "A")
dt2.Rows.Add("4", "1", "23/09/2022", "P")
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Name"), New DataColumn("Date"), New DataColumn("Status")})
Dim result = (From table1 In dt1.AsEnumerable()
Join table2 In dt2.AsEnumerable()
On table1("ID") Equals table2("ID")
Select New With {
.Name = table1("Name"),
.Date = table2("Date"),
.Status = table2("Status")
})
For Each item In result.ToList()
dt.Rows.Add(item.Name, item.Date, item.Status)
Next
ViewState("dt") = dt
BindGrid(dt)
End If
End Sub
Protected Sub Convert(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = CType(ViewState("dt"), DataTable)
Dim dt2 As DataTable = New DataTable()
dt2.Columns.Add(New DataColumn("Id"))
dt2.Columns.Add("Name")
Dim dates As List(Of String) = dt.AsEnumerable().[Select](Function(x) x("Date").ToString()).Distinct().ToList()
For i As Integer = 0 To dates.Count - 1
dt2.Columns.Add(dates(i))
Next
Dim names As List(Of String) = dt.AsEnumerable().[Select](Function(x) x("Name").ToString()).Distinct().ToList()
For i As Integer = 0 To names.Count - 1
Dim dr As DataRow = dt2.NewRow()
dr(0) = i + 1
dr(1) = names(i)
For j As Integer = 0 To dates.Count - 1
Dim row As DataRow() = dt.[Select]("Name='" & names(i) & "' AND Date='" + dates(j) & "'")
dr(j + 2) = If(row.Length > 0, row(0)("Status").ToString(), String.Empty)
Next
dt2.Rows.Add(dr)
Next
BindGrid(dt2)
End Sub
Private Sub BindGrid(ByVal dt As DataTable)
gvDetails.DataSource = dt
gvDetails.DataBind()
End Sub
Screenshot