Answered by:
Find Range Data In Gridview1 and Show in Gridview 2

Question
-
User-807418713 posted
Hello
This is my Gridview 1 data
Date Card No Description Qty Field1 Field2 Field3 Sale_Price 1-May A1 Item1 50.5 A H M 15 2-May B5 Item1 60 K Y R 55 3-May G3 Item2 90 G D M 110 4-May J2 Item1 20 B V U 50 5-May U4 Item7 110 F R D 5 6-May J4 Item4 149.5 K L P 160 7-May L4 Item5 40 R T F 100 I want to show in gridview2 Qty & Sale_Price_Range As Below
Total Record Price Range Qty_Count Sale_Price_Count 7 0-50 2 3 7 50-100 3 2 7 100-150 2 1 7 150-200 0 1 Thanking You
Sunday, August 2, 2020 7:46 AM
Answers
-
User1535942433 posted
Hi Gopi.MCA,
Accroding to your description,I'm guessing that the price range is fixed.So I use datatable to add price range into gridview.I use rowdatabound event to fill the total record and use databound event to count the qty_count,sale_price_count.
Since you don't post your codes,I create a demo,just like this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField HeaderText="Date"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("Date") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Card No"> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("Card_No") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Description"> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("Description") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Qty"> <ItemTemplate> <asp:Label ID="Label4" runat="server" Text='<%# Bind("Qty") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Field1"> <ItemTemplate> <asp:Label ID="Label5" runat="server" Text='<%# Bind("Field1") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Field2"> <ItemTemplate> <asp:Label ID="Label6" runat="server" Text='<%# Bind("Field2") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Field3"> <ItemTemplate> <asp:Label ID="Label7" runat="server" Text='<%# Bind("Field3") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Sale_Price"> <ItemTemplate> <asp:Label ID="Label8" runat="server" Text='<%# Bind("Sale_Price") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView2_RowDataBound" OnDataBound="GridView2_DataBound"> <Columns> <asp:TemplateField HeaderText="Total Record"> <ItemTemplate> <asp:Label ID="recordtotal" runat="server" Text='<%# Bind("Column1") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Price Range"> <ItemTemplate> <asp:Label ID="pricerange" runat="server" Text='<%# Bind("Column2") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Qty_Count"> <ItemTemplate> <asp:Label ID="Qty_Count" runat="server" Text='<%# Bind("Column3") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Sale_Price_Count"> <ItemTemplate> <asp:Label ID="Sale_Price_Count" runat="server" Text='<%# Bind("Column4") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Code-behind:
protected void bind() { string str, strSql; str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString; SqlConnection conn = new SqlConnection(str); strSql = "select * from Card"; SqlDataAdapter da = new SqlDataAdapter(strSql, str); DataSet ds = new DataSet(); da.Fill(ds, "Card"); this.GridView1.DataSource = ds.Tables[0].DefaultView; this.GridView1.DataBind(); conn.Close(); } protected void bind2() { DataTable dt = new DataTable(); //DataRow dr = null; dt.Columns.Add(new DataColumn("Column1", typeof(Int32))); dt.Columns.Add(new DataColumn("Column2", typeof(string))); dt.Columns.Add(new DataColumn("Column3", typeof(Int32))); dt.Columns.Add(new DataColumn("Column4", typeof(Int32))); DataRow dr = dt.NewRow(); dr["Column2"] = "1-50"; dt.Rows.Add(dr); DataRow dr1 = dt.NewRow(); dr1["Column2"] = "50.1 - 100.0"; dt.Rows.Add(dr1); DataRow dr2 = dt.NewRow(); dr2["Column2"] = "100.1- 150"; dt.Rows.Add(dr2); DataRow dr3 = dt.NewRow(); dr3["Column2"] = "150.1 -200"; dt.Rows.Add(dr3); GridView2.DataSource = dt; GridView2.DataBind(); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); bind2(); } } protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Cells[0].Text = (GridView1.Rows.Count).ToString(); } } protected void GridView2_DataBound(object sender, EventArgs e) { int qty_counta = 0; int qty_countb = 0; int qty_countc = 0; int qty_countd = 0; int Sale_Price_Counta = 0; int Sale_Price_Countb = 0; int Sale_Price_Countc = 0; int Sale_Price_Countd = 0; foreach (GridViewRow row in GridView1.Rows) { Label lbqty = (Label)row.FindControl("Label4"); if (Convert.ToDouble(lbqty.Text) >= 0 && Convert.ToDouble(lbqty.Text) <= 50) { qty_counta+=1; } GridView2.Rows[0].Cells[2].Text = qty_counta.ToString(); if (Convert.ToDouble(lbqty.Text) >= 50.1 && Convert.ToDouble(lbqty.Text) <= 100) { qty_countb += 1; } GridView2.Rows[1].Cells[2].Text = qty_countb.ToString(); if (Convert.ToDouble(lbqty.Text) >= 100.1 && Convert.ToDouble(lbqty.Text) <= 150) { qty_countc += 1; } GridView2.Rows[2].Cells[2].Text = qty_countc.ToString(); if (Convert.ToDouble(lbqty.Text) >= 150.1 && Convert.ToDouble(lbqty.Text) <= 200) { qty_countd += 1; } GridView2.Rows[3].Cells[2].Text = qty_countd.ToString(); Label lbsale = (Label)row.FindControl("Label8"); if (Convert.ToDouble(lbsale.Text) >= 0 && Convert.ToDouble(lbsale.Text) <= 50) { Sale_Price_Counta += 1; } GridView2.Rows[0].Cells[3].Text = Sale_Price_Counta.ToString(); if (Convert.ToDouble(lbsale.Text) >= 50.1 && Convert.ToDouble(lbsale.Text) <= 100) { Sale_Price_Countb += 1; } GridView2.Rows[1].Cells[3].Text = Sale_Price_Countb.ToString(); if (Convert.ToDouble(lbsale.Text) >= 100.1 && Convert.ToDouble(lbsale.Text) <= 150) { Sale_Price_Countc += 1; } GridView2.Rows[2].Cells[3].Text = Sale_Price_Countc.ToString(); if (Convert.ToDouble(lbsale.Text) >= 150.1 && Convert.ToDouble(lbsale.Text) <= 200) { Sale_Price_Countd += 1; } GridView2.Rows[3].Cells[3].Text = Sale_Price_Countd.ToString(); } }
Result:
If you have other problem,you could tell us more details of your requirment.
Best regards,
Yijing Sun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 4, 2020 5:51 AM
All replies
-
User409696431 posted
What is the relationship between the data in your first GridView and the second GridView?
In the second GridView, what is Qty_Count and what is Sale_Price_Count?
Sunday, August 2, 2020 8:21 AM -
User-807418713 posted
Hello
Check first gridview has qty column and sale-price Column from this im fetching price range.
Hope it's clear...
Thanking YouMonday, August 3, 2020 2:55 AM -
User409696431 posted
No, it is not clear. First, you have overlapping ranges. For example, 0 - 50 and 50 - 100 both include the value 50, so which range should 50 be counted in? Second, what calculation are you using from Grid1 numbers to come up with the Grid2 numbers Qty_Count and Sale_Price_Count? From your example, I don't see what you are counting that produces the numbers you show in Grid2.
Monday, August 3, 2020 2:22 PM -
User-807418713 posted
Hello
Here Is my complete format of Gridview1
No Of Record Date Card No Description Qty Field1 Field2 Field3 Sale_Price 1 1-May A1 Item1 50.5 A H M 15 2 2-May B5 Item1 60 K Y R 55 3 3-May G3 Item2 90 G D M 110 4 4-May J2 Item1 20 B V U 50 5 5-May U4 Item7 110 F R D 5 6 6-May J4 Item4 149.5 K L P 160 7 7-May L4 Item5 40 R T F 100 I want Gridview2 like this below
Total Record Price Range Qty_Count Sale_Price_Count 7 0 - 50 2 3 7 50.1 - 100.0 3 2 7 100.1- 150 2 1 7 150.1 -200 0 1 Now i will explain details how i want gridview2
Total Record : 7 (ie) total Gridview1 Record
Price Range : will be clear now
Qty Count : 0 -50 from Gridview1 Qty column has 20,40 so its 2 CountSale_Price : 0-50 from Gridview Sale_Price Column has 5,15,50 so its 3 Count
Same price range for others...? Hope now its clear?
Thanking You
Monday, August 3, 2020 7:12 PM -
User475983607 posted
The written requirement seems like a UNION query or a CTE. But the data example is not clear. It looks Qty and Sale_Price are both partitioned by the same range. Like many of your post, I think there is deeper issue with the general design.
Monday, August 3, 2020 8:31 PM -
User1535942433 posted
Hi Gopi.MCA,
Accroding to your description,I'm guessing that the price range is fixed.So I use datatable to add price range into gridview.I use rowdatabound event to fill the total record and use databound event to count the qty_count,sale_price_count.
Since you don't post your codes,I create a demo,just like this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField HeaderText="Date"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("Date") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Card No"> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("Card_No") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Description"> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("Description") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Qty"> <ItemTemplate> <asp:Label ID="Label4" runat="server" Text='<%# Bind("Qty") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Field1"> <ItemTemplate> <asp:Label ID="Label5" runat="server" Text='<%# Bind("Field1") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Field2"> <ItemTemplate> <asp:Label ID="Label6" runat="server" Text='<%# Bind("Field2") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Field3"> <ItemTemplate> <asp:Label ID="Label7" runat="server" Text='<%# Bind("Field3") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Sale_Price"> <ItemTemplate> <asp:Label ID="Label8" runat="server" Text='<%# Bind("Sale_Price") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView2_RowDataBound" OnDataBound="GridView2_DataBound"> <Columns> <asp:TemplateField HeaderText="Total Record"> <ItemTemplate> <asp:Label ID="recordtotal" runat="server" Text='<%# Bind("Column1") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Price Range"> <ItemTemplate> <asp:Label ID="pricerange" runat="server" Text='<%# Bind("Column2") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Qty_Count"> <ItemTemplate> <asp:Label ID="Qty_Count" runat="server" Text='<%# Bind("Column3") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Sale_Price_Count"> <ItemTemplate> <asp:Label ID="Sale_Price_Count" runat="server" Text='<%# Bind("Column4") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Code-behind:
protected void bind() { string str, strSql; str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString; SqlConnection conn = new SqlConnection(str); strSql = "select * from Card"; SqlDataAdapter da = new SqlDataAdapter(strSql, str); DataSet ds = new DataSet(); da.Fill(ds, "Card"); this.GridView1.DataSource = ds.Tables[0].DefaultView; this.GridView1.DataBind(); conn.Close(); } protected void bind2() { DataTable dt = new DataTable(); //DataRow dr = null; dt.Columns.Add(new DataColumn("Column1", typeof(Int32))); dt.Columns.Add(new DataColumn("Column2", typeof(string))); dt.Columns.Add(new DataColumn("Column3", typeof(Int32))); dt.Columns.Add(new DataColumn("Column4", typeof(Int32))); DataRow dr = dt.NewRow(); dr["Column2"] = "1-50"; dt.Rows.Add(dr); DataRow dr1 = dt.NewRow(); dr1["Column2"] = "50.1 - 100.0"; dt.Rows.Add(dr1); DataRow dr2 = dt.NewRow(); dr2["Column2"] = "100.1- 150"; dt.Rows.Add(dr2); DataRow dr3 = dt.NewRow(); dr3["Column2"] = "150.1 -200"; dt.Rows.Add(dr3); GridView2.DataSource = dt; GridView2.DataBind(); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); bind2(); } } protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Cells[0].Text = (GridView1.Rows.Count).ToString(); } } protected void GridView2_DataBound(object sender, EventArgs e) { int qty_counta = 0; int qty_countb = 0; int qty_countc = 0; int qty_countd = 0; int Sale_Price_Counta = 0; int Sale_Price_Countb = 0; int Sale_Price_Countc = 0; int Sale_Price_Countd = 0; foreach (GridViewRow row in GridView1.Rows) { Label lbqty = (Label)row.FindControl("Label4"); if (Convert.ToDouble(lbqty.Text) >= 0 && Convert.ToDouble(lbqty.Text) <= 50) { qty_counta+=1; } GridView2.Rows[0].Cells[2].Text = qty_counta.ToString(); if (Convert.ToDouble(lbqty.Text) >= 50.1 && Convert.ToDouble(lbqty.Text) <= 100) { qty_countb += 1; } GridView2.Rows[1].Cells[2].Text = qty_countb.ToString(); if (Convert.ToDouble(lbqty.Text) >= 100.1 && Convert.ToDouble(lbqty.Text) <= 150) { qty_countc += 1; } GridView2.Rows[2].Cells[2].Text = qty_countc.ToString(); if (Convert.ToDouble(lbqty.Text) >= 150.1 && Convert.ToDouble(lbqty.Text) <= 200) { qty_countd += 1; } GridView2.Rows[3].Cells[2].Text = qty_countd.ToString(); Label lbsale = (Label)row.FindControl("Label8"); if (Convert.ToDouble(lbsale.Text) >= 0 && Convert.ToDouble(lbsale.Text) <= 50) { Sale_Price_Counta += 1; } GridView2.Rows[0].Cells[3].Text = Sale_Price_Counta.ToString(); if (Convert.ToDouble(lbsale.Text) >= 50.1 && Convert.ToDouble(lbsale.Text) <= 100) { Sale_Price_Countb += 1; } GridView2.Rows[1].Cells[3].Text = Sale_Price_Countb.ToString(); if (Convert.ToDouble(lbsale.Text) >= 100.1 && Convert.ToDouble(lbsale.Text) <= 150) { Sale_Price_Countc += 1; } GridView2.Rows[2].Cells[3].Text = Sale_Price_Countc.ToString(); if (Convert.ToDouble(lbsale.Text) >= 150.1 && Convert.ToDouble(lbsale.Text) <= 200) { Sale_Price_Countd += 1; } GridView2.Rows[3].Cells[3].Text = Sale_Price_Countd.ToString(); } }
Result:
If you have other problem,you could tell us more details of your requirment.
Best regards,
Yijing Sun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 4, 2020 5:51 AM -
User-807418713 posted
Thank you
Tuesday, August 4, 2020 7:43 AM