Raj De Inno
Raj De Inno

Reputation: 25

how to sort in order using Datatable for Gridview

i have a table in GridView

vName   iId

Jeeva   323243
raj     4343
Abishek 3434
ramesh  4545
Manoj   7374234
viky    885

I want to show this table in ascending/decending order by using DataTable.

static string strcon = "Data Source=;Initial Catalog=;Integrated Security=True";
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter("select * from one", con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();

Upvotes: 0

Views: 1189

Answers (2)

Vinod
Vinod

Reputation: 4872

Try this:

datatable.DefaultView.Sort = "yourcolumnname ASC"; 


datatable = datatable.DefaultView.ToTable();

Upvotes: 4

Milen
Milen

Reputation: 8867

Using linq:

var OrderedItems = dt.OrderBy(x=>x.vName).ToList();

GridView1.DataSource = OrderedItems;

Or order by any other column (OrderByDescending can be used too)

Upvotes: 3

Related Questions