【问题标题】:Sorting ASP Datalist after BINDINGBINDING 后对 ASP 数据列表进行排序
【发布时间】:2012-08-27 20:10:27
【问题描述】:

我正在将 DataList 与动态值绑定(即从 google api 到特定位置的距离。)

即来自 x 位置:

10 公里以外 15公里以外等如下

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

有没有办法按升序或降序对这些值进行排序。

注意:距离不是 DB 值,它们是动态的。

这可以在 Button1_Click 中排序吗?

【问题讨论】:

    标签: datalist


    【解决方案1】:

    在玩了很多小时的代码之后,我做到了。

    以下是针对 GRIDVIEW 的,对于 DataList 也可以遵循类似的步骤。

    页面加载:我在现有数据表中添加了一个额外的列“Miles”

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

    【讨论】:

      猜你喜欢
      • 2016-08-27
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-07
      • 2017-04-19
      • 1970-01-01
      • 2011-04-16
      相关资源
      最近更新 更多