ListView with group headers
Introduction

- string LastBoardingPoint = string.Empty;
- public string AddGroupingRowIfBoardingHasChanged() {
- string values = string.Empty;
- string CurrentBoardingPoint = Eval("BUS_BOARDING_POINT").ToString();
- string boardingPointTime = Eval("START_TIME").ToString();
- string Landmark = Eval("LAND_MARK").ToString();
- if (CurrentBoardingPoint.Length == 0) {
- CurrentBoardingPoint = "Unknown";
- } else
- if (!LastBoardingPoint.Equals(CurrentBoardingPoint)) {
- LastBoardingPoint = CurrentBoardingPoint;
- values = String.Format("<tr class='groupheader'><td colspan='13'>Boarding Point : {0} - {1} ,{2} </td></tr>", CurrentBoardingPoint, boardingPointTime, Landmark);
- } else {
- values = string.Empty;
- }
- return values;
- }
By adding the preceding databinding syntax we are instructing the ListView to call the AddGroupingRowIfBoardingHasChanged() each time it renders the ItemTemplate, that it will do once per record being bound to the ListView. The AddGroupingRowIfBoardingHasChanged() method, which we will create momentarily, will return either an empty string (in the case that the supplier name hasn't changed since the last product) or the HTML for a table row that announces the new BoardingPoint.
Next, create the AddGroupingRowIfBoardingHasChanged() method in the page's code-behind class. This method must return a string value and canont be marked Private (that is, it must be marked as Public or Protected). Keep in mind that this method is called once for every record being bound to the ListView. Add the following code:
- <asp:ListView ID="lvDetails" runat="server" OnItemDataBound="lvDetails_ItemDataBound" OnDataBound="lvDetails_DataBound">
- <LayoutTemplate>
- <table width="100%" border="1px" cellpadding="0" cellspacing="0" id="tripsheet">
- <thead>
- <tr>
- <th>
- SNo
- </th>
- <th>
- Ticket No
- </th>
- <th>
- Seat No
- </th>
- <th>
- Passenger Name
- </th>
- <th>
- Contact
- </th>
- <th>
- Droping Point
- </th>
- <th>
- Fare(Rs.)
- </th>
- <th>
- Com.(Rs.)
- </th>
- <th>
- Balance(Rs.)
- </th>
- <th>
- Booked By
- </th>
- <th>
- Booking Type
- </th>
- </tr>
- </thead>
- <tbody>
- <tr id="itemPlaceHolder" runat="server">
- </tr>
- </tbody>
- <tfoot>
- <tr>
- <td colspan="5">
- </td>
- <td>
- Totals
- </td>
- <td>
- Rs.<asp:Label ID="lblTotalSum" Font-Bold="true" runat="server"></asp:Label>
- </td>
- <td>
- Rs.<asp:Label ID="lblTotalComm" Font-Bold="true" runat="server"></asp:Label>
- </td>
- <td>
- Rs.<asp:Label ID="lblTotalBalance" Font-Bold="true" runat="server"></asp:Label>
- </td>
- </tr>
- </tfoot>
- </table>
- </LayoutTemplate>
- <ItemTemplate>
- <%# AddGroupingRowIfBoardingHasChanged() %>
- <tr>
- <td>
- <%# Container.DataItemIndex+1 %>
- </td>
- <td>
- <%# Eval("PNR_NUMBER")%>
- </td>
- <td>
- <%# Eval("SEAT_NUMBER")%>
- </td>
- <td>
- <%# Eval("CUSTOMER_NAME")%>
- </td>
- <td>
- <%# Eval("CUSTOMER_CONTACT")%>
- </td>
- <td>
- <%# Eval("DROPING_POINT")%>
- </td>
- <td>
- <%# Eval("FARE")%>
- </td>
- <td>
- <%# Eval("COMMISSION")%>
- </td>
- <td>
- <%# Convert.ToInt32(Eval("FARE")) - Convert.ToInt32(Eval("COMMISSION"))%>
- </td>
- <td>
- <%# Eval("BOOKED_AT")%>
- </td>
- <td>
- <%# Eval("BOOKING_TYPE")%>
- </td>
- </tr>
- </ItemTemplate>
- <EmptyItemTemplate>
- No Bookings are made for this Service
- </EmptyItemTemplate>
- </asp:ListView>
ListView Items DataBound
The following is for the DataBound ListView Items:
- int totalfare, totalCommisionAmount, balanceAmount;
- // Calculating Totals
- protected void lvDetails_ItemDataBound(object sender, ListViewItemEventArgs e) {
- ListViewDataItem dataItem = (ListViewDataItem) e.Item;
- if (e.Item.ItemType == ListViewItemType.DataItem) {
- DataRowView rowView = (DataRowView) dataItem.DataItem;
- totalfare = totalfare + Convert.ToInt32(rowView["FARE"]);
- totalCommisionAmount = totalCommisionAmount + Convert.ToInt32(rowView["COMMISSION"]);
- balanceAmount = balanceAmount + (Convert.ToInt32(rowView["FARE"]) - Convert.ToInt32(rowView["COMMISSION"]));
- }
- }
- // Binding Totals to Labels
- protected void lvDetails_DataBound(object sender, EventArgs e) {
- Label lblTotal = (Label) lvDetails.FindControl("lblTotalSum");
- lblTotal.Text = totalfare.ToString();
- Label lblTotalCommisionAmount = (Label) lvDetails.FindControl("lblTotalComm");
- lblTotalCommisionAmount.Text = totalCommisionAmount.ToString();
- Label lblTotalBalanceAmount = (Label) lvDetails.FindControl("lblTotalBalance");
- lblTotalBalanceAmount.Text = balanceAmount.ToString();
- }
Grouping Method Based On PNR_NUMBER
The following is the grouping method based On PNR_NUMBER:
- private DataTable GenerateData(DataTable dtDetails) {
- string currentpnr = string.Empty;
- //Geting Data Table structure
- // DtDetails Have the actual data
- DataTable newData = dtDetails.Clone();
- // Getting Distinct datatable PNR_NUMBERS
- DataTable dtDistinctData = dtDetails.DefaultView.ToTable("NeWData", true, "PNR_NUMBER");
- StringBuilder sbSeats = new StringBuilder();
- foreach(DataRow drow in dtDistinctData.Rows) {
- int fareSum = 0, commitionSum = 0;
- currentpnr = Convert.ToString(drow["PNR_NUMBER"]);
- //Selecting the rows
- DataRow[] rows = dtDetails.Select("PNR_NUMBER='" + currentpnr + "'");
- //Seats join
- var result = rows.Select(row => row[3].ToString()).ToArray();
- string joinseats = string.Join(",", result);
- //fares sum
- string[] resultfare = rows.Select(row => row[11].ToString()).ToArray();
- for (int i = 0; i < resultfare.Length; i++) {
- fareSum = fareSum + Convert.ToInt32(resultfare[i]);
- }
- //commition sum
- string[] resultcomm = rows.Select(row => row[16].ToString()).ToArray();
- for (int i = 0; i < resultcomm.Length; i++) {
- commitionSum = commitionSum + Convert.ToInt32(resultcomm[i]);
- }
- //gender join
- var resultgender = rows.Select(row => row[4].ToString()).ToArray();
- string joingenders = string.Join(",", resultgender);
- // copy data to new datatable
- DataRow drrow = newData.NewRow();
- drrow["PNR_NUMBER"] = currentpnr;
- drrow["SEAT_NUMBER"] = joinseats + "(" + joingenders + ")";
- drrow["FARE"] = fareSum;
- drrow["COMMISSION"] = commitionSum;
- drrow["CUSTOMER_GENDER"] = joingenders;
- drrow["CUSTOMER_NAME"] = rows[0]["CUSTOMER_NAME"];
- drrow["CUSTOMER_CONTACT"] = rows[0]["CUSTOMER_CONTACT"];
- drrow["BUS_BOARDING_POINT"] = rows[0]["BUS_BOARDING_POINT"];
- drrow["BOOKED_AT"] = rows[0]["BOOKED_AT"];
- drrow["DROPING_POINT"] = rows[0]["DROPING_POINT"];
- drrow["START_TIME"] = rows[0]["START_TIME"];
- drrow["BOOKING_TYPE"] = rows[0]["BOOKING_TYPE"];
- drrow["LAND_MARK"] = rows[0]["LAND_MARK"];
- //Adding Rows to table
- newData.Rows.Add(drrow);
- }
- return newData;
- }
- protected void GetPassengersDetails() {
- // getting individual total booked seats from service
- DataTable dtPassengers = objBusiness.GetData(EBEnum.Enum.Functionality.Bookings.ToString(), 0, ref strProcedure);
- DataTable newdata = this.GenerateData(dtPassengers);
- lvDetails.DataSource = newdata;
- lvDetails.DataBind();
- }
That's all there is to it! With this code in place, we now get the desired grouping interface.

pawan mishraPosted Jul 17, 2017, 6:43 AM
What is the use of this line DataTable dtPassengers = objBusiness.GetData(EBEnum.Enum.Functionality.Bookings.ToString(), 0, ref strProcedure); DataTable newdata = this.GenerateData(dtPassengers);
kalu singh raoPosted Jul 11, 2016, 3:00 AM
Nice...