Windows Develop Bookmark and Share   
 index > Windows Forms General > Sort DataGrid Text Column
 

Sort DataGrid Text Column

I haveatextfieldinadatatablewhichstoresdocumentidentifiers.
Identifierscaninclude numbersornumbersandletters,i.e.'1','2','12a','2b'.
Ishowthetable rowsinaDataGridcontrol.TheproblemiswhenIsortthedatagridbyclicking
oncolumnheaderitissortedasfollow:1,12a,2,2b.ButIwantittobe
sortedinthefollowingway:1,2,2b,12a.Isthereanywaytochangethis
sortorderbehaviorinDataGridcontrol?
Thanks.
tonhinbm  Tuesday, April 03, 2007 10:48 AM

Hi,tonhinbm

Try this meathod.When you sort this column the values will not be in numeric order. This example converts the string to a number and sorts based on the number.

Sub DataGridView1_SortCompare(ByVal sender As Object, _

ByVal e As System.Windows.Forms.DataGridViewSortCompareEventArgs) _

Handles DataGridView1.SortCompare

Dim intValue1, intValue2 As Integer

If Not Integer.TryParse(e.CellValue1.ToString, intValue1) Then Return

If Not Integer.TryParse(e.CellValue2.ToString, intValue2) Then Return

If intValue1 = intValue2 Then

e.SortResult = 0

ElseIf intValue2 > intValue1 Then

e.SortResult = -1

Else

e.SortResult = 1

End If

e.Handled = True

End Sub

Hope it helps

Gavin

Gavin Jin - MSFT  Wednesday, April 04, 2007 2:39 AM
I'm workinkg with visual studio 2003 code, so I have to use a 'datagrid' not a 'datagridview'.
Thanks Gavin Jin.
tonhinbm  Wednesday, April 04, 2007 6:22 AM

You can use google to search for other answers

Custom Search

More Threads

• System.Net
• load menu dynamically
• dubieus , lost his sp1 gold
• ListView Grouping
• OpenFileDialog fails to open the specified path
• datetimepicker set value with only time
• How to split a cell in DataGridView in C#
• How Can I make the event of Activate Windows to run only one time?
• Changes to AutoScale & AutoScaleBaseSize
• Embed and update CML and SVG