Hi Vasanth057,
I have created sample that full-fill your requirement. Refer the below sample.
HTML
<asp:GridView ID="rptrbus" runat="server" AutoGenerateColumns="false" DataKeyNames="ID"
OnRowDataBound="rptrbus_RowDataBound">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField HeaderText="Seat">
<ItemTemplate>
<asp:DataList runat="server" ID="dlSeat" OnItemDataBound="dlSeat_ItemDataBound">
<ItemTemplate>
<asp:Label ID="lblSeat" Text='<%# Eval("Seat") %>' runat="server" />
<asp:Button ID="btnSave" Text="Save" runat="server" />
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)), new DataColumn("Name"), new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
rptrbus.DataSource = dt;
rptrbus.DataBind();
}
}
protected void rptrbus_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string id = rptrbus.DataKeys[e.Row.RowIndex].Value.ToString();
DataList dlseat = e.Row.FindControl("dlSeat") as DataList;
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Id", typeof(int)), new DataColumn("Seat", typeof(int)) });
dt.Rows.Add(1, 0);
dt.Rows.Add(2, 1);
dt.Rows.Add(3, 1);
dt.Rows.Add(4, 0);
DataRow[] row = dt.Select("Id=" + id);
DataTable dt1 = dt.Clone();
foreach (DataRow dr in row)
{
dt1.ImportRow(dr);
}
dlseat.DataSource = dt1;
dlseat.DataBind();
}
}
protected void dlSeat_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label seat = e.Item.FindControl("lblSeat") as Label;
Button btn = e.Item.FindControl("btnSave") as Button;
if (seat.Text.Trim() == "1")
{
btn.BackColor = System.Drawing.Color.Red;
btn.ForeColor = System.Drawing.Color.White;
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
rptrbus.DataSource = dt
rptrbus.DataBind()
End If
End Sub
Protected Sub rptrbus_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim id As String = rptrbus.DataKeys(e.Row.RowIndex).Value.ToString()
Dim dlseat As DataList = TryCast(e.Row.FindControl("dlSeat"), DataList)
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Id", GetType(Integer)), New DataColumn("Seat", GetType(Integer))})
dt.Rows.Add(1, 0)
dt.Rows.Add(2, 1)
dt.Rows.Add(3, 1)
dt.Rows.Add(4, 0)
Dim row As DataRow() = dt.[Select](Convert.ToString("Id=") & id)
Dim dt1 As DataTable = dt.Clone()
For Each dr As DataRow In row
dt1.ImportRow(dr)
Next
dlseat.DataSource = dt1
dlseat.DataBind()
End If
End Sub
Protected Sub dlSeat_ItemDataBound(sender As Object, e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim seat As Label = TryCast(e.Item.FindControl("lblSeat"), Label)
Dim btn As Button = TryCast(e.Item.FindControl("btnSave"), Button)
If seat.Text.Trim() = "1" Then
btn.BackColor = System.Drawing.Color.Red
btn.ForeColor = System.Drawing.Color.White
End If
End If
End Sub
Screenshot