2012-08-13 43 views
0

後我從x位置結合與動態值一個DataList(從谷歌API即距離從一個特定的位置。)排序ASP Datalist中BINDING

即:

10公里 15公里等作爲如下

enter image description here

使用此代碼的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)); 
    } 
} 

有沒有辦法排序在ascendind或descening w.r.t距離這些值。

注意:距離不是DB值,它們是動態的。

這可以排序在Button1_Click嗎?

回答

0

Alrite經過很多小時的代碼玩我做到了。

以下是GRIDVIEW,DataList也可以遵循類似的步驟。

頁面加載:我增加了一個額外的列 '萬里' 到已經存在的數據表

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(); 

    } 
} 

行數據綁定:

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; 
} 

按距離排序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(); 
    } 
}