473,609 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.DataS ource = ds;
GridView1.DataB ind();

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

Regards,
Raja.

Mar 26 '07 #1
2 5576
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.Def aultView;
dv.Sort = e.SortDirection ;
gridView.DataSo urce = dv;
gridView.DataBi nd();

Bruno

<si**********@g mail.comwrote in message
news:11******** **************@ e65g2000hsc.goo glegroups.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.DataS ource = ds;
GridView1.DataB ind();

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

Regards,
Raja.

Mar 27 '07 #2
On 26 Mar, 14:56, sivagurur...@gm ail.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.DataS ource = ds;
GridView1.DataB ind();

Im using the AllowSorting=Tr ue 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/
CSGridViewSorti ngPaging.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8488
by: Ken Tucker | last post by:
I've read about this issue in many articles across the net... But haven't found a solution. I see all kinds of custom code to perform sorting with datagrids, but my example is so simple, I must just be missing something. Basically, I have a .aspx page that is very simple and has a simple datagrid. All code is in the .cs page for the datagrid (hence, I'm doing all datagrid work programmatically). The datagrid is populated from a SQL table...
5
36845
by: Richard Dixson | last post by:
I created a new C# web application. Basically all I am trying to do is create a table that consists of a few rows with two columns in each. And then to set those columns to text values from my code behind. However I am not able to do this at all, I am going about this wrong, I think and need guideance. If this was just straight HTML I would do this: <table> <tr><td>field 1</td><td>value 1 set by code behind</td></tr>
5
18529
by: sck10 | last post by:
Hello, I have a GridView that is using the following to connect to a SQL Server 2000 stored procedure: <asp:SqlDataSource ID="dsWebDocList" SelectCommand="sp_web_WebDocument" ConnectionString="<%$ ConnectionStrings:cnnSQL_Connect %>" Runat="server"> <SelectParameters> <asp:Parameter Type="String" DefaultValue="FindWebDocAll"
3
12728
by: Dorte | last post by:
Hi, Could someone help me with a couple of links to SqlDatasource documentation on how to use the Gridview and SqlDatasource components in code behind? Basically I'm missing some documentation on how to handle different tasks programmatically such as selects, updates, sorting of data etc. in code behind if I prefer to set all Gridview and Sqldatasource properties etc. in code behind rather than in the aspx file.
2
11716
by: Jacksm | last post by:
How can I align an asp:table columns with gridview columns (the widths)? I have tried setting table.column(0).width = gridview.column(0).width at page_load but it doesn't work. Thanks in advance
1
2798
by: =?Utf-8?B?SGFycnkgS2Vjaw==?= | last post by:
I have a GridView that I dynamically add columns to in code. The way that my code is written, everything works properly if I use GridView.Columns.Add to simply add the columns to the end of the collection. However, if I use GridView.Columns.Insert to inject the columns in the middle of the existing columns, I see lots of exceptions when I page the GridView, because certain controls contained within the grid are mysteriously absent? Can...
2
2895
by: =?Utf-8?B?SmF5IFBvbmR5?= | last post by:
Based on wether a row is selected in a GridView I need to HIDE the last two columns of a gridview. I do NOT need to make the cells invisible I want to hide the entire column. When I set the Visible property on the columns it causes the underlying ObjectDataSource to re-issue the Select statement. How can I hide these columns without double clutching the ODS Select?
3
2362
by: RPhlb | last post by:
This is my first post, so excuse me if I don't get this right the first time. I have an issue where when I use DropDownList I only get the first, or "SelectedValue" back when I update a GridView Edit. I have been trying to hunt down a viable soltion to this, seems that many people out there have similar issues and the ...SelectedItem.Value or SelectedValue is not returning what is actually being selected by the end user. So the simple question...
1
3723
by: suganya | last post by:
Hi Can any body help me how can I change the header color of the gridview in code behind. I tried GridView1.Attributes.Add("HeaderStyle-BackColor", "blue"); But its not working.
0
8067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8567
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8527
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8398
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2529
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.