This method sorts the contents of the DataGridView by comparing values in the specified column. If direction = ListSortDirection.Ascending Then "Error: Invalid Selection", MessageBoxButtons.OK, _ MessageBox.Show("Select a single column and try again.", _ ' If no column has been selected, display an error dialog box. ' Sort a new column and remove the old SortGlyph. If oldColumn Is newColumn AndAlso dataGridView1.SortOrder = _ ' Sort the same column again, reversing the SortOrder. ' If oldColumn is null, then the DataGridView is not currently sorted. NewColumn = dataGridView1.SelectedColumns(0)ĭim oldColumn As DataGridViewColumn = dataGridView1.SortedColumn ' Check which column is selected, otherwise set NewColumn to Nothing. Private Sub SortButton_Click(ByVal sender As Object, _īyVal e As EventArgs) Handles sortButton.Click SortOrder.Ascending : SortOrder.Descending =ĭirection = ListSortDirection.Ascending ? "Error: Invalid Selection", MessageBoxButtons.OK,ĭataGridView1.Sort(newColumn, direction) MessageBox.Show("Select a single column and try again.", If no column has been selected, display an error dialog box. Sort a new column and remove the old SortGlyph. Sort the same column again, reversing the SortOrder.ĭataGridView1.SortOrder = SortOrder.Ascending)ĭirection = ListSortDirection.Descending If oldColumn is null, then the DataGridView is not currently sorted. Check which column is selected, otherwise set NewColumn to null.ĭataGridViewElementStates.Selected) = 1 ?ĭataGridViewColumn oldColumn = dataGridView1.SortedColumn private void sortButton_Click(object sender, System.EventArgs e) The following code example demonstrates how to use the Sort in a programmatic sort. The object specified by the DataSource property has a SupportsSorting property value of false. The object specified by the DataSource property does not implement the IBindingList interface. The VirtualMode property is set to true and the IsDataBound property of the specified column returns false. When you provide your own data source by implementing virtual mode, you must also handle the sorting operations yourself.Ĭalling this method automatically sets the CurrentCell property to null. When you bind the DataGridView control to an external data source, you must use the sorting operations provided by that data source. This method works only when the DataSource property is not set. When sorting by columns set to programmatic sort mode, you must display the sorting glyph yourself by setting the DataGridViewColumnHeaderCell.SortGlyphDirection property. In this case, you will typically set the DataGridViewColumn.SortMode property to DataGridViewColumnSortMode.Programmatic to disable automatic sorting and to leave room for a sorting glyph. In order to implement a highly customized sorting operation, you can write an event handler for the ColumnHeaderMouseClick event and call this method with an instance of a class that implements the interface as a parameter. This method allows advanced customization of the sorting feature of the DataGridView class. ' If the Last Names are equal, sort based on the First Name.ĭataGridViewRow1.Cells(0).Value.ToString(), _ĭataGridViewRow2.Cells(0).Value.ToString()) ' Try to sort based on the Last Name column.ĭim CompareResult As Integer = ( _ĭataGridViewRow1.Cells(1).Value.ToString(), _ĭataGridViewRow2.Cells(1).Value.ToString()) Implements ĭim DataGridViewRow1 As DataGridViewRow = CType(x, DataGridViewRow)ĭim DataGridViewRow2 As DataGridViewRow = CType(y, DataGridViewRow) Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _ Public Sub New(ByVal sortOrder As SortOrder)ĮlseIf sortOrder = sortOrder.Ascending Then Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _ĭataGridView1.Sort(New RowComparer(SortOrder.Ascending))ĭataGridView1.Sort(New RowComparer(SortOrder.Descending)) Return CompareResult * sortOrderModifier If the Last Names are equal, sort based on the First Name. Try to sort based on the Last Name column. Private static int sortOrderModifier = 1 Įlse if (sortOrder = SortOrder.Ascending)ĭataGridViewRow DataGridViewRow1 = (DataGridViewRow)x ĭataGridViewRow DataGridViewRow2 = (DataGridViewRow)y private void Button1_Click( object sender, EventArgs e )ĭataGridView1.Sort( new RowComparer( SortOrder.Ascending ) ) ĭataGridView1.Sort( new RowComparer( SortOrder.Descending ) ) In this example, the IComparer interface is implemented in the RowComparer class. The following code example demonstrates how to use the Sort method overload in a multiple column sort scenario.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |