Answered by:
Need Design In Repeater Or Gridview

Question
-
User2033107836 posted
Hello
This is my result set from SQL
BatchNo Category Color Batch Pieces Types Date ItemName Rate Qty Amount B1 Fruits All 500 SA 1-Aug-17 A 10 5 50 B1 Fruits All 500 SA 1-Aug-17 Z 5 50 250 B1 Fruits All 500 SA 1-Aug-17 C 2 4 8 B1 Fruits All 500 MP 5-Aug-17 O 2 6 12 B1 Fruits All 500 MP 5-Aug-17 L 4 6 24 B1 Fruits All 500 MP 5-Aug-17 K 5 5 25 B1 Fruits All 500 MP 5-Aug-17 B 2 3 6 I want like this design from my output like this below
* Cost Per Pieces Will Come GrandTotal Amount / Batch Pcs (ie) 375 / 500 = 0.75
How to do using repeater (or) Gridview in easy method using asp.net 2.0 C#.. Please give me easy format so that it can be usefull for others as well
Friday, August 11, 2017 7:03 AM
Answers
-
User-271186128 posted
Hi asp.ambur,
Please Give me Code in asp.net 2.0 C# using Repeater ControlYou could try to use Nested repeater control, please refer to the following code:
<style type="text/css"> table { border-collapse: collapse; } table, th, td { border: 1px solid black; } </style> <asp:Repeater ID="Repeater1" OnItemDataBound="Repeater1_ItemDataBound" runat="server"> <HeaderTemplate> <table> </HeaderTemplate> <ItemTemplate> <tr> <td>Batch No</td> <td> <asp:Label ID="lbl_BatchNo" runat="server" Text='<%# Eval("BatchNo") %>'></asp:Label></td> <td>Batch Prieces</td> <td> <asp:Label ID="lbl_BatchPieces" runat="server" Text='<%# Eval("BatchPieces") %>'></asp:Label></td> <td>Cost Per Piece</td> </tr> <tr> <td>Category</td> <td> <asp:Label ID="lbl_Category" runat="server" Text='<%# Eval("Category") %>'></asp:Label></td> <td>Color</td> <td> <asp:Label ID="lbl_Color" runat="server" Text='<%# Eval("Color") %>'></asp:Label></td> <td> <asp:Label ID="lbl_PerPiece" runat="server" Text=""></asp:Label></td> </tr> <asp:HiddenField ID="hid_expression" runat="server" Value="" /> <asp:Repeater ID="Repeater2" OnItemDataBound="Repeater2_ItemDataBound" runat="server"> <ItemTemplate> <tr> <td>Types</td> <td> <asp:Label ID="lbl_Types" runat="server" Text='<%# Eval("Types") %>'></asp:Label></td> <td>Date</td> <td> <asp:Label ID="lbl_Date" runat="server" Text='<%# Eval("Date") %>'></asp:Label></td> <td></td> </tr> <asp:Repeater ID="Repeater3" runat="server"> <HeaderTemplate> <tr> <td>Item Name</td> <td>Rate</td> <td>Qty</td> <td>Amount</td> <td></td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:Label ID="lbl_ItemName" runat="server" Text='<%# Eval("ItemName") %>'></asp:Label></td> <td> <asp:Label ID="lbl_Rate" runat="server" Text='<%# Eval("Rate") %>'></asp:Label></td> <td> <asp:Label ID="lbl_Qty" runat="server" Text='<%# Eval("Qty") %>'></asp:Label></td> <td> <asp:Label ID="lbl_PerPiece" runat="server" Text='<%# Eval("Amount") %>'></asp:Label></td> <td></td> </tr> </ItemTemplate> </asp:Repeater> <tr> <td></td> <td>Total For <asp:Label ID="lbl_TotalTypes" runat="server" Text='<%# Eval("Types") %>'></asp:Label></td> <td> <asp:Label ID="lbl_totalQty" runat="server" Text=""></asp:Label></td> <td> <asp:Label ID="lbl_totalAmount" runat="server" Text=""></asp:Label></td> <td></td> </tr> </ItemTemplate> </asp:Repeater> <tr> <td></td> <td>Grand Total</td> <td> <asp:Label ID="lbl_GrandTotalQty" runat="server" Text=""></asp:Label></td> <td> <asp:Label ID="lbl_GrandTotalAmount" runat="server" Text=""></asp:Label></td> <td></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater>
Code behind:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[10] { new DataColumn("BatchNo",typeof(string)), new DataColumn("Category",typeof(string)), new DataColumn("Color",typeof(string)), new DataColumn("BatchPieces",typeof(string)), new DataColumn("Types",typeof(string)), new DataColumn("Date",typeof(string)), new DataColumn("ItemName",typeof(string)), new DataColumn("Rate",typeof(int)), new DataColumn("Qty",typeof(int)), new DataColumn("Amount",typeof(int)) }); dt.Rows.Add("B1", "Fruits", "All", "500", "SA", "1-Aug-17", "A", 10, 5, 50); dt.Rows.Add("B1", "Fruits", "All", "500", "SA", "1-Aug-17", "Z", 5, 50, 250); dt.Rows.Add("B1", "Fruits", "All", "500", "SA", "1-Aug-17", "C", 2, 4, 8); dt.Rows.Add("B1", "Fruits", "All", "500", "MP", "5-Aug-17", "O", 2, 6, 12); dt.Rows.Add("B1", "Fruits", "All", "500", "MP", "5-Aug-17", "L", 4, 6, 24); dt.Rows.Add("B1", "Fruits", "All", "500", "MP", "5-Aug-17", "K", 5, 5, 25); dt.Rows.Add("B1", "Fruits", "All", "500", "MP", "5-Aug-17", "B", 2, 3, 6); //using DefaultView ToTable method to select columns and remove duplicate items. DataTable newdt = dt.DefaultView.ToTable(true, "BatchNo", "Category", "Color", "BatchPieces"); ViewState["Data"] = dt; Repeater1.DataSource = newdt; Repeater1.DataBind(); } } protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (ViewState["Data"] != null) { DataTable dt = (DataTable)ViewState["Data"]; if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Label lbl_BatchNo = (Label)e.Item.FindControl("lbl_BatchNo"); Label lbl_BatchPieces = (Label)e.Item.FindControl("lbl_BatchPieces"); Label lbl_Category = (Label)e.Item.FindControl("lbl_Category"); Label lbl_Color = (Label)e.Item.FindControl("lbl_Color"); Repeater rp2 = (Repeater)e.Item.FindControl("Repeater2"); if(rp2!=null) { string expression = "BatchNo ='" + lbl_BatchNo.Text + "' and Category ='" + lbl_Category.Text + "' and Color ='" + lbl_Color.Text + "' and BatchPieces ='" + lbl_BatchPieces.Text + "'"; //using a hidden field to store the filter expression. HiddenField hid_expression = (HiddenField)e.Item.FindControl("hid_expression"); hid_expression.Value = expression; // Create a DataView using the table as its source and the filter and sort expressions DataView dv = new DataView(dt, expression, null, DataViewRowState.CurrentRows); // Convert the DataView to a DataTable DataTable new_table = dv.ToTable("newtable", true, "Types", "Date"); rp2.DataSource = new_table; rp2.DataBind(); } //display the grandTotal value. Label lbl_GrandTotalQty = (Label)e.Item.FindControl("lbl_GrandTotalQty"); lbl_GrandTotalQty.Text = grandtotal_Qty.ToString(); Label lbl_GrandTotalAmount = (Label)e.Item.FindControl("lbl_GrandTotalAmount"); lbl_GrandTotalAmount.Text = grandtotal_Account.ToString(); //calculate the cost per pieve, and display the result. Label lbl_PerPiece = (Label)e.Item.FindControl("lbl_PerPiece"); lbl_PerPiece.Text = (grandtotal_Account/ Convert.ToDouble(lbl_BatchPieces.Text)).ToString(); //reset the value to 0; subtotal_Qty = subtotal_Account = grandtotal_Qty = grandtotal_Account = 0; } } } double subtotal_Qty = 0; //declare some variable to store the total value. double subtotal_Account = 0; double grandtotal_Qty = 0; double grandtotal_Account = 0; protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (ViewState["Data"] != null) { DataTable dt = (DataTable)ViewState["Data"]; if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Label lbl_Types = (Label)e.Item.FindControl("lbl_Types"); Label lbl_Date = (Label)e.Item.FindControl("lbl_Date"); HiddenField hid_expression = (HiddenField)e.Item.Parent.NamingContainer.FindControl("hid_expression"); Repeater rp3 = (Repeater)e.Item.FindControl("Repeater3"); if (rp3 != null) { string expression = hid_expression.Value + " and Types = '" + lbl_Types.Text + "' and Date='" + lbl_Date.Text + "'"; // Create a DataView using the table as its source and the filter and sort expressions DataView dv = new DataView(dt, expression, null, DataViewRowState.CurrentRows); // Convert the DataView to a DataTable DataTable new_table = dv.ToTable("newtable",true, "ItemName", "Rate", "Qty", "Amount"); rp3.DataSource = new_table; rp3.DataBind(); //using Datatable.Compute method to calculate the sum of the Qty and Amount. subtotal_Qty = Convert.ToDouble(new_table.Compute("sum(Qty)", null)); subtotal_Account = Convert.ToDouble(new_table.Compute("sum(Amount)", null)); Label lbl_totalQty = (Label)e.Item.FindControl("lbl_totalQty"); Label lbl_totalAmount = (Label)e.Item.FindControl("lbl_totalAmount"); //display the result. lbl_totalQty.Text = subtotal_Qty.ToString(); lbl_totalAmount.Text = subtotal_Account.ToString(); //calculate the grandtotal grandtotal_Qty += subtotal_Qty; grandtotal_Account += subtotal_Account; } } } }
The output as below:
Best regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, August 14, 2017 10:19 AM
All replies
-
User-1838255255 posted
Hi asp.ambur,
According to your needs and photo, I think you don't need nested the repeater or gridview. You only need use one gridview and add need rows in code behind. About how to add row, please check the following code:
Sample Code:
<div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnDataBound="GridView1_DataBound"> <Columns> <asp:BoundField DataField="ItemName" HeaderText="ItemName" ItemStyle-Width="90" /> <asp:BoundField DataField="Rate" HeaderText="Rate" ItemStyle-Width="120" /> <asp:BoundField DataField="Qty" HeaderText="Qty" ItemStyle-Width="100" /> <asp:BoundField DataField="Amount" HeaderText="Amount" ItemStyle-Width="100" /> <asp:TemplateField> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("ItemName", typeof(string)); dt.Columns.Add("Rate", typeof(string)); dt.Columns.Add("Qty", typeof(string)); dt.Columns.Add("Amount", typeof(string)); dt.Rows.Add("A", "10", "5", "50"); dt.Rows.Add("Z", "5", "50", "250"); dt.Rows.Add("C", "2", "4", "8"); dt.Rows.Add("O", "10", "2", "6"); dt.Rows.Add("L", "10", "4", "6"); GridView1.DataSource = dt; GridView1.DataBind(); } protected void GridView1_DataBound(object sender, EventArgs e) { GridViewRow gr = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal); TableCell cell = new TableCell(); cell.Text = "BatchNo"; cell.Style.Add("color", "red"); cell.ColumnSpan = 1; gr.Controls.Add(cell); cell = new TableCell(); cell.ColumnSpan = 1; cell.Text = "B1"; gr.Controls.Add(cell); cell = new TableCell(); cell.ColumnSpan = 1; cell.Style.Add("color", "red"); cell.Text = "Batch Pieces"; gr.Controls.Add(cell); cell = new TableCell(); cell.ColumnSpan = 1; cell.Text = "500"; gr.Controls.Add(cell); cell = new TableCell(); cell.ColumnSpan = 1; cell.Text = "CostPerPiece"; cell.Style.Add("color", "red"); gr.Controls.Add(cell); GridView1.HeaderRow.Parent.Controls.AddAt(0, gr); GridViewRow newgr = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal); TableCell newcell = new TableCell(); newcell.Style.Add("color", "red"); newcell.Text = "Category"; newcell.ColumnSpan = 1; newgr.Controls.Add(newcell); newcell = new TableCell(); newcell.ColumnSpan = 1; newcell.Text = "Fruits"; newgr.Controls.Add(newcell); newcell = new TableCell(); newcell.ColumnSpan = 1; newcell.Text = "Color"; newcell.Style.Add("color", "red"); newgr.Controls.Add(newcell); newcell = new TableCell(); newcell.ColumnSpan = 1; newcell.Text = "All"; newgr.Controls.Add(newcell); newcell = new TableCell(); newcell.ColumnSpan = 1; newcell.Text = "0.75"; newgr.Controls.Add(newcell); GridView1.HeaderRow.Parent.Controls.AddAt(1, newgr); // Comment: all values could get from the database, I use fixed value to replace it. //you could add one line like me after group the data, then get the last row index of this group. //at next row to append new row. }
Result:
Best Regards,
Eric Du
Saturday, August 12, 2017 10:58 AM -
User2033107836 posted
Hello Eric
See My Screenshot it has to come calculating based on my output
I think it can be achieve using repeater only.. Please check my output screenshot
Saturday, August 12, 2017 11:14 AM -
User-398246787 posted
Your screen shot is really confusing, in the long run it will be tough to understand the report.
better change the idea.
if you want this output as screenshot, then you can't do it in repeater just by binding and also with databounding event.
the only way you can obtain this output is,
iterate through your datatable, and create custom table using html and css, in the codebehind.
Saturday, August 12, 2017 1:25 PM -
User2033107836 posted
Hello
It's Okay For Me To Go With Repeater Control
Please Give me Code in asp.net 2.0 C# using Repeater Control
Saturday, August 12, 2017 1:50 PM -
User-271186128 posted
Hi asp.ambur,
Please Give me Code in asp.net 2.0 C# using Repeater ControlYou could try to use Nested repeater control, please refer to the following code:
<style type="text/css"> table { border-collapse: collapse; } table, th, td { border: 1px solid black; } </style> <asp:Repeater ID="Repeater1" OnItemDataBound="Repeater1_ItemDataBound" runat="server"> <HeaderTemplate> <table> </HeaderTemplate> <ItemTemplate> <tr> <td>Batch No</td> <td> <asp:Label ID="lbl_BatchNo" runat="server" Text='<%# Eval("BatchNo") %>'></asp:Label></td> <td>Batch Prieces</td> <td> <asp:Label ID="lbl_BatchPieces" runat="server" Text='<%# Eval("BatchPieces") %>'></asp:Label></td> <td>Cost Per Piece</td> </tr> <tr> <td>Category</td> <td> <asp:Label ID="lbl_Category" runat="server" Text='<%# Eval("Category") %>'></asp:Label></td> <td>Color</td> <td> <asp:Label ID="lbl_Color" runat="server" Text='<%# Eval("Color") %>'></asp:Label></td> <td> <asp:Label ID="lbl_PerPiece" runat="server" Text=""></asp:Label></td> </tr> <asp:HiddenField ID="hid_expression" runat="server" Value="" /> <asp:Repeater ID="Repeater2" OnItemDataBound="Repeater2_ItemDataBound" runat="server"> <ItemTemplate> <tr> <td>Types</td> <td> <asp:Label ID="lbl_Types" runat="server" Text='<%# Eval("Types") %>'></asp:Label></td> <td>Date</td> <td> <asp:Label ID="lbl_Date" runat="server" Text='<%# Eval("Date") %>'></asp:Label></td> <td></td> </tr> <asp:Repeater ID="Repeater3" runat="server"> <HeaderTemplate> <tr> <td>Item Name</td> <td>Rate</td> <td>Qty</td> <td>Amount</td> <td></td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:Label ID="lbl_ItemName" runat="server" Text='<%# Eval("ItemName") %>'></asp:Label></td> <td> <asp:Label ID="lbl_Rate" runat="server" Text='<%# Eval("Rate") %>'></asp:Label></td> <td> <asp:Label ID="lbl_Qty" runat="server" Text='<%# Eval("Qty") %>'></asp:Label></td> <td> <asp:Label ID="lbl_PerPiece" runat="server" Text='<%# Eval("Amount") %>'></asp:Label></td> <td></td> </tr> </ItemTemplate> </asp:Repeater> <tr> <td></td> <td>Total For <asp:Label ID="lbl_TotalTypes" runat="server" Text='<%# Eval("Types") %>'></asp:Label></td> <td> <asp:Label ID="lbl_totalQty" runat="server" Text=""></asp:Label></td> <td> <asp:Label ID="lbl_totalAmount" runat="server" Text=""></asp:Label></td> <td></td> </tr> </ItemTemplate> </asp:Repeater> <tr> <td></td> <td>Grand Total</td> <td> <asp:Label ID="lbl_GrandTotalQty" runat="server" Text=""></asp:Label></td> <td> <asp:Label ID="lbl_GrandTotalAmount" runat="server" Text=""></asp:Label></td> <td></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater>
Code behind:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[10] { new DataColumn("BatchNo",typeof(string)), new DataColumn("Category",typeof(string)), new DataColumn("Color",typeof(string)), new DataColumn("BatchPieces",typeof(string)), new DataColumn("Types",typeof(string)), new DataColumn("Date",typeof(string)), new DataColumn("ItemName",typeof(string)), new DataColumn("Rate",typeof(int)), new DataColumn("Qty",typeof(int)), new DataColumn("Amount",typeof(int)) }); dt.Rows.Add("B1", "Fruits", "All", "500", "SA", "1-Aug-17", "A", 10, 5, 50); dt.Rows.Add("B1", "Fruits", "All", "500", "SA", "1-Aug-17", "Z", 5, 50, 250); dt.Rows.Add("B1", "Fruits", "All", "500", "SA", "1-Aug-17", "C", 2, 4, 8); dt.Rows.Add("B1", "Fruits", "All", "500", "MP", "5-Aug-17", "O", 2, 6, 12); dt.Rows.Add("B1", "Fruits", "All", "500", "MP", "5-Aug-17", "L", 4, 6, 24); dt.Rows.Add("B1", "Fruits", "All", "500", "MP", "5-Aug-17", "K", 5, 5, 25); dt.Rows.Add("B1", "Fruits", "All", "500", "MP", "5-Aug-17", "B", 2, 3, 6); //using DefaultView ToTable method to select columns and remove duplicate items. DataTable newdt = dt.DefaultView.ToTable(true, "BatchNo", "Category", "Color", "BatchPieces"); ViewState["Data"] = dt; Repeater1.DataSource = newdt; Repeater1.DataBind(); } } protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (ViewState["Data"] != null) { DataTable dt = (DataTable)ViewState["Data"]; if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Label lbl_BatchNo = (Label)e.Item.FindControl("lbl_BatchNo"); Label lbl_BatchPieces = (Label)e.Item.FindControl("lbl_BatchPieces"); Label lbl_Category = (Label)e.Item.FindControl("lbl_Category"); Label lbl_Color = (Label)e.Item.FindControl("lbl_Color"); Repeater rp2 = (Repeater)e.Item.FindControl("Repeater2"); if(rp2!=null) { string expression = "BatchNo ='" + lbl_BatchNo.Text + "' and Category ='" + lbl_Category.Text + "' and Color ='" + lbl_Color.Text + "' and BatchPieces ='" + lbl_BatchPieces.Text + "'"; //using a hidden field to store the filter expression. HiddenField hid_expression = (HiddenField)e.Item.FindControl("hid_expression"); hid_expression.Value = expression; // Create a DataView using the table as its source and the filter and sort expressions DataView dv = new DataView(dt, expression, null, DataViewRowState.CurrentRows); // Convert the DataView to a DataTable DataTable new_table = dv.ToTable("newtable", true, "Types", "Date"); rp2.DataSource = new_table; rp2.DataBind(); } //display the grandTotal value. Label lbl_GrandTotalQty = (Label)e.Item.FindControl("lbl_GrandTotalQty"); lbl_GrandTotalQty.Text = grandtotal_Qty.ToString(); Label lbl_GrandTotalAmount = (Label)e.Item.FindControl("lbl_GrandTotalAmount"); lbl_GrandTotalAmount.Text = grandtotal_Account.ToString(); //calculate the cost per pieve, and display the result. Label lbl_PerPiece = (Label)e.Item.FindControl("lbl_PerPiece"); lbl_PerPiece.Text = (grandtotal_Account/ Convert.ToDouble(lbl_BatchPieces.Text)).ToString(); //reset the value to 0; subtotal_Qty = subtotal_Account = grandtotal_Qty = grandtotal_Account = 0; } } } double subtotal_Qty = 0; //declare some variable to store the total value. double subtotal_Account = 0; double grandtotal_Qty = 0; double grandtotal_Account = 0; protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (ViewState["Data"] != null) { DataTable dt = (DataTable)ViewState["Data"]; if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Label lbl_Types = (Label)e.Item.FindControl("lbl_Types"); Label lbl_Date = (Label)e.Item.FindControl("lbl_Date"); HiddenField hid_expression = (HiddenField)e.Item.Parent.NamingContainer.FindControl("hid_expression"); Repeater rp3 = (Repeater)e.Item.FindControl("Repeater3"); if (rp3 != null) { string expression = hid_expression.Value + " and Types = '" + lbl_Types.Text + "' and Date='" + lbl_Date.Text + "'"; // Create a DataView using the table as its source and the filter and sort expressions DataView dv = new DataView(dt, expression, null, DataViewRowState.CurrentRows); // Convert the DataView to a DataTable DataTable new_table = dv.ToTable("newtable",true, "ItemName", "Rate", "Qty", "Amount"); rp3.DataSource = new_table; rp3.DataBind(); //using Datatable.Compute method to calculate the sum of the Qty and Amount. subtotal_Qty = Convert.ToDouble(new_table.Compute("sum(Qty)", null)); subtotal_Account = Convert.ToDouble(new_table.Compute("sum(Amount)", null)); Label lbl_totalQty = (Label)e.Item.FindControl("lbl_totalQty"); Label lbl_totalAmount = (Label)e.Item.FindControl("lbl_totalAmount"); //display the result. lbl_totalQty.Text = subtotal_Qty.ToString(); lbl_totalAmount.Text = subtotal_Account.ToString(); //calculate the grandtotal grandtotal_Qty += subtotal_Qty; grandtotal_Account += subtotal_Account; } } } }
The output as below:
Best regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, August 14, 2017 10:19 AM -
Monday, August 14, 2017 5:25 PM