排序数据表在C#ASP.NET中不起作用?

时间:2018-08-30 12:49:46

标签: c# asp.net sorting datatable

我正在尝试对数据表进行排序,但是它正在工作。我想按distance列进行排序。 这是我的输出img

我想按ASC的顺序,从最低到最高排序。

代码:

  SqlConnection cnn = new SqlConnection(connection);
        string query = "SELECT PostCode,Latitude,Longitude FROM [kaykocou_admin].[kaykocou_admin].[Deals]";
        SqlCommand cmd = new SqlCommand(query,cnn);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adp.Fill(dt);
       DataTable dtnew = new DataTable();
       DataRow _ravi;
        if (dt.Rows.Count>0)
        {

            dtnew.Clear();
            dtnew.Columns.Add("PostCode");
            dtnew.Columns.Add("Lat");
            dtnew.Columns.Add("Long");
            // dtnew.Columns.Add("Lat 1");
            // dtnew.Columns.Add("Long 1");
            dtnew.Columns.Add("distance");
             string str2 = GET("http://api.postcodes.io/postcodes/SW1A 0AA");
             var serializer2 = new JavaScriptSerializer();
             dynamic deserialize_post2 = serializer2.DeserializeObject(str2);

             decimal Longitude1 = deserialize_post2["result"]["longitude"];
             decimal Latitude1 = deserialize_post2["result"]["latitude"];


            foreach (DataRow dr in dt.Rows)
            {


       string postcode = dr["PostCode"].ToString();
       // string str1 = GET("http://api.postcodes.io/postcodes/" + postcode);
       // string final = str1.Replace(@"{", "");
       // string final2 = final.Replace(@"}", "");
       // str1 = str1.Replace('[', ' ');
       // str1 = str1.Replace(']', ' ');
       // string result = "{ " + final2 + " }";
       // var serializer = new JavaScriptSerializer();
       // dynamic deserialize_post = serializer.DeserializeObject(str1);

        decimal Longitude =Convert.ToDecimal( dr["Longitude"].ToString());
        decimal Latitude =Convert.ToDecimal( dr["Latitude"].ToString()) ;

        _ravi = dtnew.NewRow();


        var sCoord = new GeoCoordinate(Convert.ToDouble(Latitude), Convert.ToDouble(Longitude));
        var eCoord = new GeoCoordinate(Convert.ToDouble(Latitude1), Convert.ToDouble(Longitude1));
        var distance=sCoord.GetDistanceTo(eCoord);
        _ravi["PostCode"] = postcode;
        _ravi["Lat"] = dr["Longitude"].ToString();
          _ravi["Long"]= dr["Longitude"].ToString();
          _ravi["distance"] =Convert.ToDouble( Math.Round(((distance / 1000.0) * 0.621371192), 2));

        dtnew.Rows.Add(_ravi);
            }
            DataView view = dtnew.DefaultView;
            view.Sort = "distance ASC";
            DataTable sortedDate = view.ToTable();
            //dtnew.DefaultView.Sort = "distance";
            gr.DataSource = sortedDate;
            gr.DataBind();
        }

    }

0 个答案:

没有答案