Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>According to <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.sortcompare%28v=vs.100%29.aspx" rel="nofollow">MSDN -DataGridView.SortCompare Event</a>, The SortCompare will be triggered only for columns that have their "SortMode" property set to "Automatic":</p> <pre><code>DataGridViewColumn col = this.dataGridView1.Columns["IsActive"]; col.SortMode = DataGridViewColumnSortMode.Automatic; </code></pre> <p>If I may, I would suggest letting .Net doing the dirty job. Lets assume you have 3 rows in your grid view:</p> <pre> Id Price IsActive 1 1 1 2 2 1 3 11 1 </pre> <p>In the way you have implemented the sorting, if you sort by price, row 3 will precede row 2 (the string "11" comes before "2"...). Preferably you would have your data in a data table, bind the datatable to the gridview and let .Net do the rest:</p> <pre><code> /// &lt;summary&gt; /// Binds the Grid view. /// &lt;/summary&gt; private void BindGridView() { //Creating the columns. //The gridview will know how to sort Items by the type specified in the second argment DataTable dt = new DataTable(); dt.Columns.Add("Id",typeof(int)); dt.Columns.Add("Price",typeof(int)); dt.Columns.Add("IsActive",typeof(bool)); //Creating some random data //Replace this with your actual data... Random rnd = new Random(1); for (int i = 0; i &lt; 100; i++) { int Id = i+1; int Price = Id%2 == 0? 500-Id*2:350+Id*3; bool isActive = (Id%5) !=0; DataRow row = dt.NewRow(); row["Id"] =Id ; row["Price"] = rnd.Next(1000) ; row["IsActive"] = isActive; dt.Rows.Add(row); } this.dataGridView1.DataSource = dt; //making sure all columns are sortable foreach (DataGridViewColumn col in this.dataGridView1.Columns) { col.SortMode = DataGridViewColumnSortMode.Automatic; } } </code></pre>
 

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