473,659 Members | 3,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP.NET DataGridView Sorting Problem

24 New Member
OK, this is rediculous...

Surely, sorting should be one of the easiest things with the ASP.NET DataGridView control. I mean, there's hundreds of sites out there (Microsoft MSDN entries included) which explain and give examples of the DataGridView sorting and how to make it work...

SO WHY CAN'T I GET IT TO WORK?! Dammit!

Apologies for the rant. I'll try and keep it to the point from here on in:

I have tried the following:
  • I have an object with a "ListComponents " function that I wrote to return a DataTable for my data source. When sorting didn't work using that, I tried...
  • ...researching it a bit, so I checked out the MSDN stuff (http://msdn.microsoft. com/en-us/library/system.web.ui.w ebcontrols.grid view.sortexpres sion.aspx) and basically copied the code (including the SqlDataSource), except substituding my connection string and SQL select statements where applicable...
  • ... This didn't work, so I went back to my original idea and tried mucking about with If Not IsPostBacks and various other things...
  • ... This didn't work, so I looked on the web some more, and everyone seems to be saying that it should just WORK!

I've had a good amount of success on this site, so I'm hoping someone will be able to help me.

Here is my code (for what it's worth)..:

ASP Code:
Expand|Select|Wrap|Line Numbers
  1. <asp:DataGrid AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" ID="dgvComponents" runat="server">
  2.     <Columns>
  3.         <asp:BoundColumn HeaderText="ID" DataField="ID" Visible="False" ReadOnly="true" SortExpression="ID"></asp:BoundColumn>
  4.         <asp:BoundColumn HeaderText="Part Reference" DataField="SPARE_REF" ReadOnly="true" SortExpression="SPARE_REF"></asp:BoundColumn>
  5.         <asp:BoundColumn HeaderText="Part Name" DataField="NAME" ReadOnly="true" SortExpression="NAME"></asp:BoundColumn>
  6.     </Columns>
  7. </asp:DataGrid>
  8.  
Code Behind:
Expand|Select|Wrap|Line Numbers
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.  
  3.     dgvComponents.DataSource = Component.ListComponents()
  4.     dgvComponents.DataBind()
  5.  
  6. End Sub
  7.  
And, like, Component.ListC omponents basically just returns a DataTable with the following as the select string:
Expand|Select|Wrap|Line Numbers
  1. SELECT ID, NAME, SPARE_REF FROM COMPONENTS
Incidentally, I also tried adding a "sortExpression " parameter to ListComponents, e.g.:
Expand|Select|Wrap|Line Numbers
  1. ListComponents("ORDER BY ID ASC")
When I mentioned that I was playing around with IsPostBack, here's an example:
Expand|Select|Wrap|Line Numbers
  1. If Not IsPostBack Then
  2.      dgvComponents.DataSource = Component.ListComponents()
  3. End If
  4.  
(I thought perhaps I'm only supposed to set the DataSource first-time-round. This actually made the whole table disappear when I click a header...!

With regards to the results I get, basically, you click on a header (either NAME or PART REFERENCE) and nothing happens. I'd expect it to alternate between Ascending and Descending =)

Thanks for your time, in advance.

-Q
Oct 10 '08 #1
0 1493

Sign in to post your reply or Sign up for a free account.

Similar topics

10
26993
by: rob | last post by:
I have a class that among others exposes a string property "Date". The date in this property is stored in the form yyyymmdd. Now I do the following 1) Generate a DataGridViewTextBoxColumn column for that property 2) Add the column to the datagridview 3) Populate a BindingSource by adding all instances of the above mentioned class. 4) Assign the BindingSource to the datagridview's DataSource property
4
8628
by: Matt | last post by:
I have been searching all over the web for a way to sort a DataGridView based on the actual text being shown in a ComboBox column as opposed to the underlying value (an ID in this case). Can anyone tell me if this is even possible, and if so, how to do it? If this is completely impossible, how would you suggest going about sorting a ComboBox wherein the text displayed in the column is the client's name, and the underlying value is an ID? ...
4
18704
by: shibeta | last post by:
Hello, I have problem with DataGridView and BindingSource. I have created DataSet and added TableData to it with manualy created columns (without DataAdapter - I'm not using MSSQL). On the Form I put DataGridView and BindingSource, and connected them (BindingSource of course is also connected with DataSet). Next on the separated thread I'm reading data from DB and load them do DataSet (more preciselly, readed data I've saved as XML and...
0
3906
by: mahesh.nimbalkar | last post by:
I want to use Multi column sorting in DataGridView. The scenario is like this: 1) DataGridView has two columns; Id and name 2) User clicks on Id column and DataGridView is sorted ascending 3) User clicks on Id column and DataGridView is sorted descendin Now, when user clicks on name column, the DataGridView should be sorted as Id desc and name asc.
1
9667
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I'm using a DataGridView on a Form. I want to disable the clicking on the columns headers to disallow the sorting. How can I do that, but without deriving the DataGridView? Is there any other way to disable the sorting of the DataGridView without deriving it?
8
7430
by: Kyote | last post by:
On my form I have a datagridview that's bound to a table on my DB. When I run the app then click on a column header to sort, it sorts just fine. But my problem is a result of that sort. Even though the view has been sorted, it's underlying data isn't. After sorting I'm trying to get the datarow I just doubleclicked on. The problem is it's returning the pre sorted row. That is, the row that was there before I sorted the column. Dim...
4
4787
by: ReneMarxis | last post by:
Hello i have one problem with sorting in an unbound DataGridView. I set sorting to automatic but i get errors when having DBNull values in the column that gets sort ("Object must be of type string"). I solved that problem for one grid/column by fetching the _SortCompare event and doing the sort manualy by seting the SortResult accordingly if its a null value (-1) or not (e.Handled=false;return;) Is there some possibility to specify...
6
8767
by: Simon Harvey | last post by:
Hi all, I'm really hoping someone can help me with this as it's causing me some serious problems. I have a Windows Forms application using the gridview control. When the user selects a row, the SelectionChanged event fires and I load some more data that's particular to that row. The problem I'm having is that the SelectionChanged event is going way
7
20002
Plater
by: Plater | last post by:
I am having trouble determining when my DataGridView object is sorting (based on a column header click). The idea is, in a large table, sorting the columns takes time, so I show a splash screen. When it is done sorting I want the splash screen dissapear. What I had been doing was using the CellClick and Sorted events: private void myDGV_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex == -1) {//sorting
0
8748
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
8628
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...
1
6181
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...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
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
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
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.