public void SortTest1() { SetupTestList(); m_cgSorter.Sort("StrProp", false); Assert.AreEqual("item1", ((DummyListItem)m_list[0]).StrProp); Assert.AreEqual("item2", ((DummyListItem)m_list[1]).StrProp); Assert.AreEqual("item3", ((DummyListItem)m_list[2]).StrProp); Assert.AreEqual("item4", ((DummyListItem)m_list[3]).StrProp); Assert.AreEqual(500, ((DummyListItem)m_list[0]).IntProp); Assert.AreEqual(800, ((DummyListItem)m_list[1]).IntProp); Assert.AreEqual(100, ((DummyListItem)m_list[2]).IntProp); Assert.AreEqual(200, ((DummyListItem)m_list[3]).IntProp); Assert.AreEqual(dummyEnum.One, ((DummyListItem)m_list[0]).EnumProp); Assert.AreEqual(dummyEnum.Two, ((DummyListItem)m_list[1]).EnumProp); Assert.AreEqual(dummyEnum.Three, ((DummyListItem)m_list[2]).EnumProp); Assert.AreEqual(dummyEnum.One, ((DummyListItem)m_list[3]).EnumProp); Assert.AreEqual("01/21/2008", ((DummyListItem)m_list[0]).DateProp.ToString("MM/dd/yyyy")); Assert.AreEqual("01/24/2008", ((DummyListItem)m_list[1]).DateProp.ToString("MM/dd/yyyy")); Assert.AreEqual("01/23/2008", ((DummyListItem)m_list[2]).DateProp.ToString("MM/dd/yyyy")); Assert.AreEqual("01/24/2008", ((DummyListItem)m_list[3]).DateProp.ToString("MM/dd/yyyy")); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Sorts the specified column. /// </summary> /// <param name="column">The column on which to sort. When this is the column that /// is already the sorted column, then use toggleDirection to specify whether or /// not the column should be sorted in reverse order.</param> /// <param name="toggleDirection">true to toggle the sort direction when column is /// already the current sort column. Otherwise, false. When column is not the current /// sort column, then this flag is ignored. /// </param> /// <param name="defaultSortCol">The index of the column to sort on when there /// isn't any sort information that can be read from the registry.</param> /// ------------------------------------------------------------------------------------ protected void Sort(DataGridViewColumn column, bool toggleDirection, int defaultSortCol) { if (m_dataGridView.Columns.Count == 0) { return; } using (new WaitCursor(Parent)) { object prevSelectedRow = GetPreSortRow(); // If the sort column is specified, then add that column to the sort order // and sort. If the column is not specified, then sort on the sort order // saved in the registry. If no values are found in the registry, then sort // on the reference column. if (column != null) { m_gridSorter.Sort(column.DataPropertyName, toggleDirection); } else if (m_persistence != null && !m_gridSorter.ReadSortInfoFromReg(m_persistence.SettingsKey)) { column = m_dataGridView.Columns[defaultSortCol]; m_gridSorter.Sort(column.DataPropertyName, toggleDirection); } else { m_gridSorter.Sort(); // Find the column associated with the primary sort // field (i.e. the first field in the sort order. foreach (DataGridViewColumn col in m_dataGridView.Columns) { if (col.DataPropertyName == m_gridSorter.PrimarySortProperty) { column = col; break; } } } if (m_persistence != null) { m_gridSorter.WriteSortInfoToReg(m_persistence.SettingsKey); } // Force the grid to repaint now that the list is reordered. m_dataGridView.Refresh(); // Update the glyph on the heading of the sorted column. column.HeaderCell.SortGlyphDirection = m_gridSorter.PrimarySortDirection; m_sortedColumn = column; // Need to clear the SortGlyph on the other columns foreach (DataGridViewColumn otherColumn in m_dataGridView.Columns) { if (otherColumn != column) { otherColumn.HeaderCell.SortGlyphDirection = SortOrder.None; } } // Select the row that was previously selected. RestorePreSortRow(prevSelectedRow); } m_dataGridView.Cursor = Cursors.Default; }