Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Guys !!

Actually My Requirment is to creating Gridview dynamically and bind data in grid view and generating another gridview same time on the previous grid view row again and again...


Binding Gridview Dynamically from code Behind In asp.net from database table
Generating another Gridview on from above gridview row index if data find.

What I have tried:

My Code.....
<asp:GridView ID="GridView1" runat="server" /> 
 
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;
}
}
 
 
 
Table ----------
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

Please solve this urgently...
Posted
Updated 17-Nov-17 13:44pm
v3
Comments
Karthik_Mahalingam 16-Nov-17 7:38am    
not clear, Use Improve question to add more info to the question.
Vivek VIshwas 16-Nov-17 23:38pm    
I have to create a grid view dynamically from database and bind the grid view with data. In my above sql table there are multiple cID, i want to create automatically another grid view from from parent grid view row index id row id contains multiple record, means i want to create a nested grid view on generated gridview row index.
Karthik_Mahalingam 17-Nov-17 0:14am    
so on click of parent gridview row you need to populate the child gridview with the related items ?
Vivek VIshwas 17-Nov-17 0:21am    
of course !

Let me explain once again. I have a table, there is 5 column in that , Srno, Pid, Pname, CId, cName, cQty, and Pdetails. In my table cid is repeated more than once in Pid. I took a gridview to bind the all data from data base table. Now, gridview binding successfully, its fine. Now what i want suppose in gridview of first row id is 15 ( that is pid which is uniique in cId column ) how to generate another grid view to show data where pid=15 from table. and this will be happening again and again if any row id is repeating in Pid column . Now, tell me how to solve
Karthik_Mahalingam 17-Nov-17 1:15am    
you need a separate gridview to display the child details?

1 solution

refer this example
<form id="form1" runat="server">
     
        Parent Data
        <asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" autogenerateselectbutton="True"></asp:GridView>
        Child Data
        <asp:GridView ID="GridView2" runat="server"  ></asp:GridView>
    </form>


protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack) {
               DataTable dt = GetParentData();  // Get the Parent Data
               GridView1.DataSource = dt;
               GridView1.DataBind();

           }
       }



       protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
       {
           GridViewRow row = GridView1.SelectedRow;
           string pid = row.Cells[0].ToString();
           DataTable dtChild = GetChildData(pid);  // // Get the Child Data based on the Parent ID selected
           GridView2.DataSource = dtChild;
           GridView2.DataBind();

       }
 
Share this answer
 
Comments
Vivek VIshwas 17-Nov-17 1:47am    
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// DataTable dt = GetParentData(); // Get the Parent Data
// GridView1.DataSource = dt;
// GridView1.DataBind();
BindGrid();
}
}



protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
string pid = row.Cells[4].ToString();






string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand("Select * from TopBill where Pid="+pid+""))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}

//DataTable dtChild = GetChildData(pid); // // Get the Child Data based on the Parent ID selected
//GridView2.DataSource = dtChild;
//GridView2.DataBind();

}
private void BindGrid()
{
string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand("Select * from TopBill where Pid=0"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
Vivek VIshwas 17-Nov-17 1:48am    
check this one and solve the issues
Karthik_Mahalingam 17-Nov-17 1:59am    
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}

it should be gridview2 in child section
Vivek VIshwas 17-Nov-17 2:54am    
no it didn't help at all.
Karthik_Mahalingam 17-Nov-17 2:55am    
show your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900