Sorting the Gridview Columns via Code behind

S

sivagururaja

Hi All,

How can i sorting the Gridview Columns via the code behind.
When i tried to sorting the column it doesn't work.

SqlConnection con = new SqlConnection("Connection
string");
con.Open();
SqlCommand cmd = new SqlCommand(str, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

Im using the AllowSorting=True and SortExperssion.
What's the mistake did i made?

Regards,
Raja.
 
B

Bruno Piovan

You need to handle the Sorting event of the GridView control, in this event
you are responsible for sorting the data and binding it back to the gridview
control.

something like:

DataView dv = myDataTable.DefaultView;
dv.Sort = e.SortDirection;
gridView.DataSource = dv;
gridView.DataBind();

Bruno
 
M

mark4asp

Hi All,

How can i sorting the Gridview Columns via the code behind.
When i tried to sorting the column it doesn't work.

SqlConnection con = new SqlConnection("Connection
string");
con.Open();
SqlCommand cmd = new SqlCommand(str, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

Im using the AllowSorting=True and SortExperssion.
What's the mistake did i made?

Regards,
Raja.

Have a look at Ryan Olson's code here.

<http://forums.asp.net/thread/1254336.aspx>
<http://community.strongcoders.com/content/
CSGridViewSortingPaging.aspx>

To sort the gridview remember that you must:

get the data to which the gridview will be bound.

get that data as a DataView.

sort it on a field name (from your datasource) either as ascending or
descending.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top