Hello Guys !!
Actually, My Requirement is to create a Gridview dynamically and bind data in grid view and generating another gridview same time on the previous grid view row again and again...
1.Binding Gridview Dynamically from code Behind In asp.net from database table
2.Generating another Gridview on from above gridview row index if data find.
My Code
<asp:GridView ID="GridView1" runat="server" />
Codebehind(.cs)
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString.ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetData();
}
}
private void GetData()
{
String query = "Select * from TopBill where Pid=0";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dtMasterBill = new DataTable();
sda.Fill(dtMasterBill);
if (dtMasterBill.Rows.Count > 0)
{
GridView1.DataSource = dtMasterBill;
GridView1.DataBind();
}
else
{
// fieldReptr.Visible = false;
}
}?
CREATE TABLE [dbo].[TopBill](
[srNo] [int] IDENTITY(1,1) NOT NULL,
[Pid] [int] NULL,
[PName] [nvarchar](max) NULL,
[PDetails] [nvarchar](max) NULL,
[cId] [int] NULL,
[Cname] [nvarchar](max) NULL,
[Cqty] [nvarchar](max) NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[TopBill] ON
INSERT [dbo].[TopBill] ([srNo], [Pid], [PName], [PDetails], [cId], [Cname], [Cqty]) VALUES (1, 0, N'', N'optional', 10, N'India', N'')
INSERT [dbo].[TopBill] ([srNo], [Pid], [PName], [PDetails], [cId], [Cname], [Cqty]) VALUES (2, 10, N'India', N'optional', 1010, N'Delhi', N'1')
INSERT [dbo].[TopBill] ([srNo], [Pid], [PName], [PDetails], [cId], [Cname], [Cqty]) VALUES (4, 0, N'', N'optional', 11, N'Uk', N'')
INSERT [dbo].[TopBill] ([srNo], [Pid], [PName], [PDetails], [cId], [Cname], [Cqty]) VALUES (6, 0, N'', N'optional', 12, N'USA', N'')
INSERT [dbo].[TopBill] ([srNo], [Pid], [PName], [PDetails], [cId], [Cname], [Cqty]) VALUES (7, 0, N'', N'optional', 13, N'Canada', N'')
INSERT [dbo].[TopBill] ([srNo], [Pid], [PName], [PDetails], [cId], [Cname], [Cqty]) VALUES (8, 13, N'Canada', N'optional', 1310, N'Canada-A', N'10')
INSERT [dbo].[TopBill] ([srNo], [Pid], [PName], [PDetails], [cId], [Cname], [Cqty]) VALUES (12, 15, N'America', N'', 1510, N'New Jursy', N'10')
INSERT [dbo].[TopBill] ([srNo], [Pid], [PName], [PDetails], [cId], [Cname], [Cqty]) VALUES (15, 0, N'', N'', 16, N'bihar', N'')
INSERT [dbo].[TopBill] ([srNo], [Pid], [PName], [PDetails], [cId], [Cname], [Cqty]) VALUES (16, 1010, N'Delhi', N'optional', 101010, N'Preet Vihar', N'320')
INSERT [dbo].[TopBill] ([srNo], [Pid], [PName], [PDetails], [cId], [Cname], [Cqty]) VALUES (9, 13, N'Canada', N'optional', 1311, N'Canada-B', N'12')
INSERT [dbo].[TopBill] ([srNo], [Pid], [PName], [PDetails], [cId], [Cname], [Cqty]) VALUES (10, 0, N'', N'', 14, N'Itley', N'')
INSERT [dbo].[TopBill] ([srNo], [Pid], [PName], [PDetails], [cId], [Cname], [Cqty]) VALUES (11, 0, N'', N'', 15, N'America', N'')
INSERT [dbo].[TopBill] ([srNo], [Pid], [PName], [PDetails], [cId], [Cname], [Cqty]) VALUES (13, 15, N'America', N'', 1511, N'New Jersy', N'10')
INSERT [dbo].[TopBill] ([srNo], [Pid], [PName], [PDetails], [cId], [Cname], [Cqty]) VALUES (14, 1510, N'New Jursy', N'', 151010, N'Cat', N'12121')
SET IDENTITY_INSERT [dbo].[TopBill] OFF
?
You can do something like this for
1.Binding Gridview Dynamically from code Behind In asp.net from database table
-> Use the code below it will work(tested)
Default.aspx code
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
Code behind code (.cs)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridviewData();
}
}
private void BindGridviewData()
{
//Create a datatable
DataTable dt = new DataTable();
//Add columns to it which you want here i have added only one
dt.Columns.Add("BlogName", typeof(string));
//create a row class to add rows dynamically
DataRow dtrow = dt.NewRow();
//create database connection
using (var connection = new SqlConnection("Your Connection String"))
{
using (var command = connection.CreateCommand())
{
//query to select rows from database table Name Blogs
command.CommandText = "Select * from Blogs";
connection.Open();
using (var reader = command.ExecuteReader())
{
var indexOfColumn1 = reader.GetOrdinal("Name");
//loop through each rows from datatabe and it in DataTable
while (reader.Read())
{
dtrow = dt.NewRow();
dtrow["BlogName"] = reader.GetValue(indexOfColumn1); ; //Bind Data to Columns
dt.Rows.Add(dtrow);
}
}
connection.Close();
}
}
//bind your data to GridView
GridView1.DataSource = dt;
GridView1.DataBind();
}
I have tried the above code and it works.
2. For "Generating another Gridview on from above gridview row index if data find."
-> Repeat above process by taking row id and fetch grid view data again as described(You may have to change query, GridView id, etc, which you need to bind )
Hope this helps
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly