Dynamically Create Grid View on another Gridview row Index


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;
}
}?
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...

Asked by:- VivekVishwas
0
: 4503 At:- 11/16/2017 9:27:56 AM
asp.net C# SQL grid-view

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 1
By : VivekVishwas - at :- 11/17/2017 5:19:17 AM






1 Answers
profileImage Answered by:- vikas_jk

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

0
At:- 11/16/2017 10:48:14 AM
No, Its not helpful. Please re-read the question and improve your answer. 0
By : VivekVishwas - at :- 11/16/2017 11:57:17 AM
Thanks but "1.Binding Gridview Dynamically from code Behind In asp.net from database table??" Doesn't it relate to what was needed? as much as i understand you are asking to bind grid view from database and get it dynamically(checking C# code). Your 2nd requirement is based on 1st one only, you need to fetch id and repeat process 1 again If not, please always explain question like no one knows it other than you, in more & more detail with output images(if possible) that you need or explain things more clearly by writing your query in detail, copy-pasting code will not help us understand it Thanks 0
By : vikas_jk - at :- 11/16/2017 12:05:21 PM
Good question @Vivek, but it is not very much explained, when you are asking questions like this, you need to spend your time to explain your issues to other users deeply, so that you can take advantage of other users knowledge and get some help related to code or get some idea to proceed, because no one knows what you issues are facing on your pc & what is your expected end result, question description area is the only way to explain it in detail, so use it as much as possible :) 0
By : Sam - at :- 11/16/2017 12:20:17 PM
Yes! You all are right. 0
By : VivekVishwas - at :- 11/17/2017 5:12:05 AM
Do not use Hindi, this is not Hindi forum. Go with @Vikas's answer, you just need to check row Id and get it's data again, you can do it using foreach loop, check if multiple ids exists, get data from database(as explained in answer 1) and append your result in Child Grid(separate from First grid), if not, loop for next. If you are looking for exact code, unfortunately, it difficult to provide and work on your assignment, this is a big ask from any user 0
By : Sam - at :- 11/17/2017 8:39:46 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use