Skip to main content

ListView with group headers

In previews Post I was Explained about  Easy Way Running Background Tasks in ASP.NET , How to download a file in ASP.Net , Detecting Mobile Devices In asp.net Using  51Degrees.mobi components  

Now I am in this post Explained How to Use Listview With Group Headers 
 
ListView with group headers
Let's say we want to implement a ListView with group headers, where each group is defined by a certain data column having the same value. In other words, each time the data column has a new value (proceeding from top to bottom), we want to have a special view that highlights that fact.

Introduction

The ListView control renders its ItemTemplate once for every item in its DataSource. As discussed in Grouping Data with the ListView Control, it is possible to insert a grouping template every N records. This flexibility allows for scenarios like displaying data in a multi-column table.

While the built-in grouping feature is certainly useful, when it comes to displaying data most people think of grouping to mean that records with similar attributes are lumped together. For instance, the Ticket Booking database contains information about an assortment of products, and each product has attributes likep PNR_NUMBER, NAME, MOBILE_NO, BOOKED_BY, BOARDING_POINT, FARE and so forth. While each NAME name is unique, many PNR_NO share the same Boarding point  and  BoardingPointTime. When someone says, "I want to group the PNR_NUMBER data," usually they mean they want to group it by one of these common attributes. The following screenshot shows the user interface people most people associate with the term grouping.

Capture.JPG

tripsheet
Unfortunately the ListView's grouping feature does not allow for this style of grouping. The good news is that with a few lines of code and markup we can construct such an interface. This article shows how to build a flexible grouping interface that allows the user to choose what data field to group the data by. Read on to learn more!

    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, that 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">
                                            &nbsp;
                                        </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.

Comments

Popular posts from this blog

ASP.NET e-Commerce website GridView with Product Listing

Introduction : E-Commerce web applications are everywhere these days, and many share a common set of functionality. In this article, I will show how to use the GridView and ListView controls to build a powerful product page with many of the features found on today's e-commerce sites. We'll build a bicycle store product grid using some free clip art bicycle images. The example files are user controls which can be easily added to a page. We're only using three images here to keep the size of the sample application small. In previously I was explained about  Sending Email from Asp.net With Dynamic Content  ,  How To Export gridview data to Excel/PDF , CSV Formates in asp.net C#  , How to print Specific Area in asp.net web page How To- Search records or data in gridview using jQuery  . A shopping cart application would require to display the products in a multi column grid, rather than a straight down list or a table. The each item in a product grid would have

How to hide url parameters in asp.net

There are different ways to Hide the URL in asp.net , you can choose any one from bellow options . Previously I was Explained about the  Difference between Convert.tostring and .tostring() method Example  ,   Reasons to use Twitter Bootstrap , How to Register AJAX toolkit in web.config file in asp.net a) Using Post Method b) Using Of Session . c) URL Encoding & decoding process . d) Using Server.Transfer() instead of Response.Redirect() method (1)Use a form and POST the information. This might require additional code in source pages, but should not require logic changes in the target pages (merely change Request.QueryString to Request.Form). While POST is not impossible to muck with, it's certainly less appealing than playing with QueryString parameters. (2)Use session variables to carry information from page to page. This is likely a more substantial effort compared to (1), because you will need to take session variable checking into account (e.g. the

Nested GridView in ASP.NET Using c# with show/hide

In This example shows how to create Nested GridView In Asp.Net Using C# And VB.NET With Expand Collapse Functionality. Previous post I was Explained about the   ASP.NET e-Commerce website GridView with Product Listing  ,  How To Export gridview data to Excel/PDF , CSV Formates in asp.net C# , Sending Email from Asp.net With Dynamic Content  ,  SQL Server- Case Sensitive Search in SQL Server I have used JavaScript to Create Expandable Collapsible Effect by displaying Plus Minus image buttons. Customers and Orders Table of Northwind Database are used to populate nested GridViews. Drag and place SqlDataSource from toolbox on aspx page and configure and choose it as datasource from smart tags Go to HTML source of page and add 2 TemplateField in <Columns>, one as first column and one as last column of gridview. Place another grid in last templateField column. Markup of page after adding both templatefields will like as shown below. HTML SOURCE < a