Answered by:
Bind Gridview From Repater Data

Question
-
User-807418713 posted
Hello
This is my repeater control
<asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound"> <HeaderTemplate> <tr > <th style="text-align:center">ItemName</th> <th style="text-align:center">Type</th> <th style="text-align:center">%</th> <th style="text-align:center">Rate</th> <th style="text-align:center">Qty</th> <th>Amount</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <%# Eval("ItemName") %> </td> <td style="text-align:center"> <%# Eval("Type")%> </td> <td style="text-align:center"> <%# Eval("Perc")%> </td> <td style="text-align: center;"> <asp:Label ID="LR" runat="server" Text='<%# Eval("Rate") %>'></asp:Label> </td> <td style="text-align: center;"> <asp:Label ID="LQ" runat="server" Text='<%# Eval("Qty") %>' style="text-align: right;"></asp:Label> </td> <td style="text-align: right;"> <asp:Label ID="Label3" runat="server" Text='<%# ((Eval("Qty")!= DBNull.Value ? Convert.ToDouble(Eval("Qty")) : 0) * (Eval("Rate")!= DBNull.Value ? Convert.ToDouble(Eval("Rate")) : 0)).ToString("N2")%>'></asp:Label> </td> </tr> </ItemTemplate> <FooterTemplate> <tr> <td > </td> <td > </td> <td colspan="2" > <span style="color: Red;font-family: Palatino Linotype"><strong> Total: </strong></span> </td> <td style="text-align: right;"> <asp:Label ID="FLA" runat="server" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Blue"></asp:Label> </td> <td style=" text-align: right;"> <asp:Label ID="FLabel3" runat="server" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Blue"></asp:Label> </td> <td> </td> <td> </td> <td> </td> </tr> </FooterTemplate> </asp:Repeater>
It shows like this below
Item Name Type % Rate Qty Item1 A 0.1 10 1 Item2 C 0.2 20 2 Item3 B 0.5 30 1 Item4 B 0.9 40 1.5 Item5 A 0.9 10 2 Item1 A 0.1 5 2.5 Item4 C 0.6 10 4 Item6 C 0.4 20 4.5 Item1 D 0.4 30 2.5 I want this in gridview1
Type % Qty A 1.1 5.5 B 1.4 2.5 C 1.2 10.5 D 0.4 2.5 Thanking You
Sunday, August 23, 2020 1:12 PM
Answers
-
User-939850651 posted
Hi Gopi.MCA,
According to your description, have you tried to change the query statement to achieve your requirements?
Something like this:
create table testtbl( [ItemName] varchar(50),[Type] char(1), [%] float ,[Rate] int,[Qty] float, ) insert into testtbl values ('Item1','A',0.1,10,1),('Item2','C',0.2,20,2), ('Item3','B',0.5,30,1),('Item4','B',0.9,40,1.5),('Item5','A',0.9,10,2), ('Item1','A',0.1,5,2.5),('Item4','C',0.6,10,4),('Item6','C',0.4,20,4.5), ('Item1','D',0.4,30,2.5) select [Type],sum([%]) as Perc,sum([Qty]) as [Qty] from testtbl group by [Type] order by [Type]
Result:
On the other hand, in the page code, you may need to comment out the columns that will not be displayed, such as ItemName, Rate, etc.
Hope this can help you.
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, August 24, 2020 2:43 AM
All replies
-
User-939850651 posted
Hi Gopi.MCA,
According to your description, have you tried to change the query statement to achieve your requirements?
Something like this:
create table testtbl( [ItemName] varchar(50),[Type] char(1), [%] float ,[Rate] int,[Qty] float, ) insert into testtbl values ('Item1','A',0.1,10,1),('Item2','C',0.2,20,2), ('Item3','B',0.5,30,1),('Item4','B',0.9,40,1.5),('Item5','A',0.9,10,2), ('Item1','A',0.1,5,2.5),('Item4','C',0.6,10,4),('Item6','C',0.4,20,4.5), ('Item1','D',0.4,30,2.5) select [Type],sum([%]) as Perc,sum([Qty]) as [Qty] from testtbl group by [Type] order by [Type]
Result:
On the other hand, in the page code, you may need to comment out the columns that will not be displayed, such as ItemName, Rate, etc.
Hope this can help you.
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, August 24, 2020 2:43 AM -
User-807418713 posted
Hello Sir
I Want using asp.net C# not via ms sql query..
In page load repeater shows data from that i want to show summary in grid view.
Thanking You
Monday, August 24, 2020 3:54 AM -
User-939850651 posted
Hi Gopi.MCA,
As you can see, if you want to fulfill your requirements, the amount column will not be displayed correctly and you need to use the same method to calculate it.
protected void Page_Load(object sender, EventArgs e)
{
RepeaterBind();
}
protected void RepeaterBind() { DataTable dt = new DataTable(); string conStr = ConfigurationManager.ConnectionStrings["conStr"].ToString(); using (SqlConnection conn = new SqlConnection(conStr)) { //string query = "select [Type],sum([%]) as Perc,sum([Rate]) as [Rate],sum([Qty]) as [Qty] from testtbl group by [Type] order by [Type]"; string query = "select * from testtbl order by [Type]"; using (SqlCommand cmd = new SqlCommand(query, conn)) { SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); for (int i = dt.Rows.Count - 1; i > 0; i--) { if (dt.Rows[i]["Type"].ToString() == dt.Rows[i - 1]["Type"].ToString()) { dt.Rows[i]["Type"] = ""; decimal type1 = decimal.Parse(dt.Rows[i]["Prec"].ToString() == "" ? "0" : dt.Rows[i]["Prec"].ToString()); decimal type2 = decimal.Parse(dt.Rows[i - 1]["Prec"].ToString() == "" ? "0" : dt.Rows[i - 1]["Prec"].ToString()); dt.Rows[i - 1]["Prec"] = type1 + type2; dt.Rows[i]["Prec"] = DBNull.Value; decimal Qty1 = decimal.Parse(dt.Rows[i]["Qty"].ToString() == "" ? "0" : dt.Rows[i]["Qty"].ToString()); decimal Qty2 = decimal.Parse(dt.Rows[i - 1]["Qty"].ToString() == "" ? "0" : dt.Rows[i - 1]["Qty"].ToString()); dt.Rows[i - 1]["Qty"] = Qty1 + Qty2; dt.Rows[i]["Qty"] = DBNull.Value; } } Repeater2.DataSource = dt; Repeater2.DataBind(); } } }Result:
Or I misunderstood what you mean, please feel free let me know.
Best regards,
Xudong Peng
Tuesday, August 25, 2020 10:33 AM