Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When <strong>DataGridView</strong> binds to the <strong>DataSource</strong> (<em>DataView, BindingSource, Table, DataSet+"tablename"</em>) in all cases it refere to the <strong>DataView</strong>. Get reference to this DataView and set <strong>Sort</strong> (and <strong>Filter</strong>) as you wish:</p> <pre><code>DataView dv = null; CurrencyManager cm = (CurrencyManager)(dgv.BindingContext[dgv.DataSource, dgv.DataMember]); if (cm.List is BindingSource) { // In case of BindingSource it may be chain of BindingSources+relations BindingSource bs = (BindingSource)cm.List; while (bs.List is BindingSource) { bs = bs.List as BindingSource; } if (bs.List is DataView) { dv = bs.List as DataView; } } else if (cm.List is DataView) { // dgv bind to the DataView, Table or DataSet+"tablename" dv = cm.List as DataView; } if (dv != null) { dv.Sort = "somedate desc, firstname"; // dv.Filter = "lastname = 'Smith' OR lastname = 'Doe'"; // You can Set the Glyphs something like this: int somedateColIdx = 5; // somedate int firstnameColIdx = 3; // firstname dgv.Columns[somedateColIdx].HeaderCell.SortGlyphDirection = SortOrder.Descending; dgv.Columns[firstnameColIdx].HeaderCell.SortGlyphDirection = SortOrder.Ascending; } </code></pre> <p>Note: <strong>Column names used in Sort and Filter correspond to the column names in DataTable</strong>, Column names in DataGridView are the names of controls used to display cells in dgv. You can get column name used in DataView like this:</p> <pre><code>string colName = dgv.Columns[colIdx].DataPropertyName </code></pre> <p>Depends of how do you want to track sorted columns (colSequence, colName, asc/desc, dgvColIdx) you may decide how to build Sort and Filter expression and set the SortGlyph in dgv (I made hardcode for simplicity).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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