# Find Range Data In Gridview1 and Show in Gridview 2[Answered] RSS

## 7 replies

Last post Aug 04, 2020 07:43 AM by Gopi.MCA

• Gopi.MCA

Member

254 Points

592 Posts

### Find Range Data In Gridview1 and Show in Gridview 2

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

• KathyW

Contributor

5521 Points

2275 Posts

### Re: Find Range Data In Gridview1 and Show in Gridview 2

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?

• Gopi.MCA

Member

254 Points

592 Posts

### Re: Find Range Data In Gridview1 and Show in Gridview 2

Hello

Check first gridview has qty column and sale-price Column from this im fetching price range.

Hope it's clear...

Thanking You
• KathyW

Contributor

5521 Points

2275 Posts

### Re: Find Range Data In Gridview1 and Show in Gridview 2

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.

• Gopi.MCA

Member

254 Points

592 Posts

### Re: Find Range Data In Gridview1 and Show in Gridview 2

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 Count

Sale_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

• mgebhard

All-Star

49161 Points

21745 Posts

### Re: Find Range Data In Gridview1 and Show in Gridview 2

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.

• yij sun

Participant

1780 Points

754 Posts

### Re: Find Range Data In Gridview1 and Show in Gridview 2

Aug 04, 2020 05:51 AM|yij sun|LINK

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>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Card_No") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Qty") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Field1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("Field2") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("Field3") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<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>
<ItemTemplate>
<asp:Label ID="recordtotal" runat="server" Text='<%# Bind("Column1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<ItemTemplate>
<asp:Label ID="pricerange" runat="server" Text='<%# Bind("Column2") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Qty_Count" runat="server" Text='<%# Bind("Column3") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<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";
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;

DataRow dr = dt.NewRow();
dr["Column2"] = "1-50";

DataRow dr1 = dt.NewRow();
dr1["Column2"] = "50.1 - 100.0";

DataRow dr2 = dt.NewRow();
dr2["Column2"] = "100.1- 150";

DataRow dr3 = dt.NewRow();
dr3["Column2"] = "150.1 -200";

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

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
• Gopi.MCA

Member

254 Points

592 Posts