Hi AnkitPal,
Check this example. Now please take its reference and correct your code. To achieve this you have to use Repeater inside DataList control.
SQL
CREATE TABLE Test(Id INT,EmpCode VARCHAR(50),InsttAmt DECIMAL)
INSERT INTO Test VALUES(1,'C01AD123456',500.00)
INSERT INTO Test VALUES(2,'C01AD123456',100.00)
INSERT INTO Test VALUES(3,'C01AD123456',600.00)
INSERT INTO Test VALUES(4,'C01AD123456',200.00)
INSERT INTO Test VALUES(5,'B01AD123456',400.00)
INSERT INTO Test VALUES(6,'B01AD123456',500.00)
INSERT INTO Test VALUES(7,'B01AD123456',300.00)
INSERT INTO Test VALUES(8,'B01AD123456',600.00)
INSERT INTO Test VALUES(9,'K01AD123456',500.00)
INSERT INTO Test VALUES(10,'K01AD123456',200.00)
INSERT INTO Test VALUES(11,'K01AD123456',100.00)
INSERT INTO Test VALUES(12,'K01AD123456',800.00)
HTML
<asp:DataList runat="server" ID="dlEmployees" OnItemDataBound="dlEmployees_ItemDataBound">
<ItemTemplate>
<asp:Label ID="lblEmpCode" Text='<%# Eval("EmpCode") %>' runat="server" />
<asp:Repeater ID="rptInsttAmt" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblInsttAmt" Text='<%# Eval("InsttAmt", "{0:N2}")%>' runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:DataList>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = GetData("SELECT DISTINCT EmpCode FROM Test");
dlEmployees.RepeatColumns = dt.Rows.Count;
dlEmployees.DataSource = dt;
dlEmployees.DataBind();
}
}
protected void dlEmployees_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string empCode = (e.Item.FindControl("lblEmpCode") as Label).Text;
Repeater rptInsttAmt = e.Item.FindControl("rptInsttAmt") as Repeater;
rptInsttAmt.DataSource = GetData("SELECT InsttAmt FROM Test WHERE EmpCode = '" + empCode + "'");
rptInsttAmt.DataBind();
}
}
private DataTable GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As DataTable = GetData("SELECT DISTINCT EmpCode FROM Test")
dlEmployees.RepeatColumns = dt.Rows.Count
dlEmployees.DataSource = dt
dlEmployees.DataBind()
End If
End Sub
Protected Sub dlEmployees_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim empCode As String = (TryCast(e.Item.FindControl("lblEmpCode"), Label)).Text
Dim rptInsttAmt As Repeater = TryCast(e.Item.FindControl("rptInsttAmt"), Repeater)
rptInsttAmt.DataSource = GetData("SELECT InsttAmt FROM Test WHERE EmpCode = '" & empCode & "'")
rptInsttAmt.DataBind()
End If
End Sub
Private Function GetData(ByVal query As String) As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Screenshot