Hi ShaniKhan,
As per your requirement you need to use two table one productMaster table and second ProductDetails table.ProductMaster table store the only product details like ProductID and ProductName.ProductDetails table store the stock details like productName,stock and color name.I have given the below example sample code.So please refer the below code and modify as per your requirement.
HTML
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvProduct" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="Details" OnClick="OnClick" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<div id="divProduct" runat="server" visible="false">
ProductName:<asp:Label ID="lblProductName" runat="server" />
<br />
<asp:Panel ID="pnlcolor" runat="server">
</asp:Panel>
</div>
</div>
</form>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Data();
}
}
private void Data()
{
DataTable dt = new DataTable("ProductMaster");
dt.Columns.Add("ProductId", typeof(int));
dt.Columns.Add("ProductName");
dt.Rows.Add(1, "Chai");
dt.Rows.Add(2, "Chang");
dt.Rows.Add(3, "Aniseed Syrup");
gvProduct.DataSource = dt;
gvProduct.DataBind();
}
protected void OnClick(object sender, EventArgs e)
{
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
string productName = row.Cells[2].Text;
divProduct.Visible = true;
DataTable dt = new DataTable("ProductDetails");
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("ProductName");
dt.Columns.Add("ProductColor");
dt.Columns.Add("Stock", typeof(int));
dt.Rows.Add(1, "Chai", "Red", 10);
dt.Rows.Add(2, "Chang", "Green", 50);
dt.Rows.Add(3, "Chang", "Red", 10);
dt.Rows.Add(4, "Aniseed Syrup", "Red", 14);
dt.Rows.Add(1, "Chai", "Green", 20);
EnumerableRowCollection<DataRow> query = from ProductDetails in dt.AsEnumerable()
where ProductDetails.Field<string>("ProductName") == productName
select ProductDetails;
DataTable view = (query.AsDataView()).ToTable();
lblProductName.Text = productName;
string space = "<br/>";
foreach (DataRow item in view.Rows)
{
Label lblcolor = new Label();
Label lblstock = new Label();
lblcolor.Text = "Color:" + item.ItemArray[2].ToString() + space;
lblstock.Text = "Stock:" + item.ItemArray[3].ToString() + space;
pnlcolor.Controls.Add(lblcolor);
pnlcolor.Controls.Add(lblstock);
}
}
Vb.Net
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
Data()
End If
End Sub
Private Sub Data()
Dim dt As New DataTable("ProductMaster")
dt.Columns.Add("ProductId", GetType(Integer))
dt.Columns.Add("ProductName")
dt.Rows.Add(1, "Chai")
dt.Rows.Add(2, "Chang")
dt.Rows.Add(3, "Aniseed Syrup")
gvProduct.DataSource = dt
gvProduct.DataBind()
End Sub
Protected Sub OnClick(sender As Object, e As EventArgs)
Dim row As GridViewRow = TryCast(TryCast(sender, LinkButton).NamingContainer, GridViewRow)
Dim productName As String = row.Cells(2).Text
divProduct.Visible = True
Dim dt As New DataTable("ProductDetails")
dt.Columns.Add("Id", GetType(Integer))
dt.Columns.Add("ProductName")
dt.Columns.Add("ProductColor")
dt.Columns.Add("Stock", GetType(Integer))
dt.Rows.Add(1, "Chai", "Red", 10)
dt.Rows.Add(2, "Chang", "Green", 50)
dt.Rows.Add(3, "Chang", "Red", 10)
dt.Rows.Add(4, "Aniseed Syrup", "Red", 14)
dt.Rows.Add(1, "Chai", "Green", 20)
Dim query As EnumerableRowCollection(Of DataRow) = From ProductDetails In dt.AsEnumerable() Where ProductDetails.Field(Of String)("ProductName") = productNameProductDetails
Dim view As DataTable = (query.AsDataView()).ToTable()
lblProductName.Text = productName
Dim space As String = "<br/>"
For Each item As DataRow In view.Rows
Dim lblcolor As New Label()
Dim lblstock As New Label()
lblcolor.Text = Convert.ToString("Color:" + item.ItemArray(2).ToString()) & space
lblstock.Text = Convert.ToString("Stock:" + item.ItemArray(3).ToString()) & space
pnlcolor.Controls.Add(lblcolor)
pnlcolor.Controls.Add(lblstock)
Next
End Sub
Screenshot