Hi mukesh1,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="grdUser" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string str = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
SqlConnection conn = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
grdUser.DataSource = dt;
grdUser.DataBind();
Panel[] Panel = new Panel[grdUser.Rows.Count];
HtmlGenericControl div;
for (int i = 0; i < grdUser.Rows.Count; i++)
{
div = new HtmlGenericControl("div");
div.ID = "dv" + i;
div.Style.Add(HtmlTextWriterStyle.Height, "20px");
div.Style.Add(HtmlTextWriterStyle.Width, "300px");
div.InnerText = grdUser.Rows[i].Cells[1].Text;
Panel[i] = new Panel();
Panel[i].ID = "pnl" + i;
Panel[i].Style.Add(HtmlTextWriterStyle.Height, "20");
Panel[i].Style.Add(HtmlTextWriterStyle.Width, "300px");
Panel[i].Controls.Add(div);
form1.Controls.Add(Panel[i]);
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim str As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
Dim conn As SqlConnection = New SqlConnection(str)
Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", conn)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
grdUser.DataSource = dt
grdUser.DataBind()
Dim Panel As Panel() = New Panel(grdUser.Rows.Count - 1) {}
Dim div As HtmlGenericControl
For i As Integer = 0 To grdUser.Rows.Count - 1
div = New HtmlGenericControl("div")
div.ID = "dv" & i
div.Style.Add(HtmlTextWriterStyle.Height, "20px")
div.Style.Add(HtmlTextWriterStyle.Width, "300px")
div.InnerText = grdUser.Rows(i).Cells(1).Text
Panel(i) = New Panel()
Panel(i).ID = "pnl" & i
Panel(i).Style.Add(HtmlTextWriterStyle.Height, "20")
Panel(i).Style.Add(HtmlTextWriterStyle.Width, "300px")
Panel(i).Controls.Add(div)
form1.Controls.Add(Panel(i))
Next
End If
End Sub
Screenshot