Note that there are some explanatory texts on larger screens.

plurals
  1. PODataGridView SortCompare event doesn't fire
    text
    copied!<p>Using VS2008, C# and .NET 3.5</p> <p>I am using databound DataGridView controls to display tabular data read from a web service. In several cases, there is a numeric column that needs to be sorted. I have tried a couple of different ways to get this to work, but the column still ends up sorting alphabetically (ie, 1, 10, 2, 3 instead of 1, 2, 3, 10).</p> <p>Setting the column data type to int doesn't work for databound controls, so the only real way to do this is to provide some custom sort logic.</p> <p>Many people have suggested hooking in to the SortCompare event to provide custom sort logic, but for some reason the event code never runs -- I can put a breakpoint in the handler and it never gets there. I am adding the event handler through the GUI, so the handler is added to the control by VS, not manually.</p> <p>Here's the event handler code, lifted from somewhere around here:</p> <pre><code> private void uxLicensedSoftwareDataGridView_SortCompare( object sender, DataGridViewSortCompareEventArgs e ) { int intValue1, intValue2; if ( !Int32.TryParse( e.CellValue1.ToString(), out intValue1 ) ) return; if ( !Int32.TryParse( e.CellValue2.ToString(), out intValue2 ) ) return; if ( intValue1 == intValue2 ) e.SortResult = 0; else if ( intValue1 &lt; intValue2 ) e.SortResult = -1; else e.SortResult = 1; e.Handled = true; } </code></pre> <p>If this ever fired, it would do exactly what I want it to do. What could I be missing?</p> <p>Thanks for pointing out the (hopefully) obvious... Dave</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload