Thursday, October 15, 2009

Sort DataTable

SqlConnection cn = new SqlConnection("Data Source=mahmad\\sqlexpress;Initial Catalog=emp;user id=sa;password=sql;");
cn.Open();
SqlCommand cmd = new SqlCommand("Select City_Id,City_Name From Cities");
cmd.Connection = cn;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);

ds.Tables[0].DefaultView.Sort = "City_Name";

DataTable dtblCities = ds.Tables[0].DefaultView.ToTable();

No comments: