469,646 Members | 1,152 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

Sorting the Gridview Columns via Code behind

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.

Mar 26 '07 #1
2 5396
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

<si**********@gmail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
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.

Mar 27 '07 #2
On 26 Mar, 14:56, sivagurur...@gmail.com wrote:
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.

Mar 27 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ken Tucker | last post: by
5 posts views Thread by Richard Dixson | last post: by
5 posts views Thread by sck10 | last post: by
3 posts views Thread by Dorte | last post: by
2 posts views Thread by Jacksm | last post: by
1 post views Thread by =?Utf-8?B?SGFycnkgS2Vjaw==?= | last post: by
2 posts views Thread by =?Utf-8?B?SmF5IFBvbmR5?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.