Search code examples
datalist

Sorting ASP Datalist after BINDING


I am binding a DataList with dynamic values (ie distances from google api From a particular location.)

ie from x location :

10 km away 15 km away etc as follows

enter image description here

Using this code in ItemDataBound :

private void bindDataList(string location)
{
  DataSet dstProperty = Tbl_PropertyMaster.getPropertiesByLocation(location);
  dlstNearbyProperties.DataSource = dstProperty;
  dlstNearbyProperties.DataBind();
}

.

protected void dlstNearbyProperties_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item ||
         e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Label lblPropId = (Label)e.Item.FindControl("lblPropId");
        Label lblKmAway = (Label)e.Item.FindControl("lblKmAway");
        Label lblPrice = (Label)e.Item.FindControl("lblPrice");
        DataSet dstEnabledStat = Tbl_PropertyMaster.GetPropertyDetailsbyId(Convert.ToInt32(lblPropId.Text));
        if (dstEnabledStat.Tables[0].Rows.Count > 0)
        {
            //string origin = "8.5572357 ,76.87649310000006";
            string origin = InitialOrigin;
            string destination = dstEnabledStat.Tables[0].Rows[0]["Latitude"].ToString() + "," + dstEnabledStat.Tables[0].Rows[0]["Longitude"].ToString();
            lblKmAway.Text = devTools.getDistance(origin, destination) + " Away";
        }
        lblPrice.Text = getMinnimumOfRoomPrice(Convert.ToInt32(lblPropId.Text));
   }
}

Is there a way to sort these value in ascendind or descening w.r.t distances .

NB: Distances are not DB values,they are dynamic.

Can this be sorted in a Button1_Click ?


Solution

  • Alrite after lot of hours of playing with codes I did it.

    The following is for GRIDVIEW,Similar steps can be followed for DataList As well.

    Page Load : I added an extra column 'Miles' to the already existing datatable

    protected void Page_Load(object sender, EventArgs e)
    {
        dtbl = Tbl_PropertyMaster.SelectAllPropertyAndUserDetails().Tables[0];
        dtbl.Columns.Add("Miles", typeof(int));
        //userId = devTools.checkAdminLoginStatus();
        if (!IsPostBack)
        {
            fillDlPhotoViewAll();
            FillGrProperty();
    
        }
    }
    

    Row Data Bound :

    protected void grProperty_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    
        DataSet dstEnabledStat = Tbl_PropertyMaster.GetPropertyDetailsbyId(PropId);
        if (dstEnabledStat.Tables[0].Rows.Count > 0)
        {
            string origin = InitialOrigin;
                string destination = dstEnabledStat.Tables[0].Rows[0]["Latitude"].ToString() + "," + dstEnabledStat.Tables[0].Rows[0]["Longitude"].ToString();
                decimal Kilometre=0.00M;
                if(devTools.getDistance(origin, destination)!=0)
                {
                Kilometre=Convert.ToDecimal(devTools.getDistance(origin, destination))/1000;
                }
                lblmiles.Text = Kilometre.ToString() + "Kms";
                dtbl.Rows[inn]["Miles"] = Convert.ToInt32(devTools.getDistance(origin, destination));
                inn = inn + 1;
        }
    }
    ViewState["dtbl"] = dtbl;
    }
    

    Sort by distance Button_Click:

    protected void btnSort_Click(object sender, EventArgs e)
    {
        DataTable dataTable;
        dataTable = (DataTable)ViewState["dtbl"];
        if (dataTable.Rows.Count > 0)
        {
            dataTable.DefaultView.Sort = "Miles DESC";
            dataTable.AcceptChanges();
            grProperty.DataSource = dataTable;
            grProperty.DataBind();
        }
    }