Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Bound DataGridView Sorting: One-shot sorting
 

Bound DataGridView Sorting: One-shot sorting

My clients want to sort a bound datagridview on command only, so that adding and editing rows does not change the row positions. The DGV is data-bound. It seems that whether I sort the DataView, the BindingSource, or the column(s), I cannot stop the autosort from happening without losing the existing sorting.

One method would be to manage my own collection of DataRows; is there a better way?

---Mike
Mike Berro  Thursday, September 15, 2005 7:22 PM
If anyone cares, I ended up cloning the table, and then populating it using a sorted DataView. That way I can keep the grid databound, and still sort on demand.

---Mike
Mike Berro  Wednesday, September 21, 2005 2:34 AM
If anyone cares, I ended up cloning the table, and then populating it using a sorted DataView. That way I can keep the grid databound, and still sort on demand.

---Mike
Mike Berro  Wednesday, September 21, 2005 2:34 AM
I'm experiencing the same problem. Based from my sample project, it always sort the items in the first column whenever i'm done updating or adding new rows. Eventhough all the columns were set to "notsortable".

Hoping for clarification if this is a bug of DGV or if there's any workable solution aside from the post above.

Thanks,
rikimaru
KONJIRO  Friday, September 22, 2006 6:38 AM
I'm having the same problem, but I'm having a hard time understanding how your solution was implemented. Can you provide some moredetail on how you accomplished this?
Sculli  Tuesday, October 03, 2006 8:45 PM

This is how we solved theproblem withautomatic sorting after editing/adding records to a DataGridView.

We created a new column on the table, called 'Index', and then added it as a hidden, boundcolumn on the DataGridView.After everyprogrammatic sort,we reset the values in that column to reflect the current sort order.We then programmatically sort by the 'Index' column. This way thedatagridview is actually sorted bycolumn who's header was clicked, but editing and adding records to the datagridwill only automatically resort the 'Index'column, which will not change until the next column header is clicked.

Here is the code:

private DataGridViewColumn currentSortColumn;

private string currentSortColumnName;

private string currentSortDirection = "ASC";

private void lineItemDataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)

{

currentSortColumn = lineItemDataGridView.Columns[e.ColumnIndex];

SortOrder sortOrder = lineItemDataGridView.SortOrder;

String columnName = currentSortColumn.DataPropertyName;

//Set the sortColumn and sortDirection variables.

if (currentSortColumnName == columnName)

currentSortDirection = (currentSortDirection == "ASC" ? "DESC" : "ASC");

else

currentSortDirection = "ASC";

currentSortColumnName = columnName;

//Perform the sort

myBindingSource.Sort = currentSortColumnName + " " + currentSortDirection;

//We need tostopany events from firing as we change the values in the 'Index' column.

bindUnbindDataTables(false);

//Set the "Index" column to match whatever we just sorted by

if (columnName != "Index")

{

foreach (DataGridViewRow lineItemRow in lineItemDataGridView.Rows)

{

myBindingSource.Position = lineItemRow.Index;

lineItemDataGridView.Rows[lineItemRow.Index].Cells["_Index"].Value = lineItemRow.Index;

}

}

bindUnbindDataTables(true);

//Re-Sort by the "Index" column (which will match the sort order we are currently in)

myBindingSource.Sort = "Index";

lineItemDataGridView.ClearSelection();

lineItemDataGridView[0, 0].Selected = true;

lineItemDataGridView.FirstDisplayedScrollingRowIndex = 0;

lineItemDataGridView.ResumeLayout();

//Set the Glyph on the Column we have sorted by (this has to come after the 'Index' column sort or the Glyph disappears)

currentSortColumn.HeaderCell.SortGlyphDirection = (currentSortDirection == "ASC" ? SortOrder.Ascending : SortOrder.Descending);

}

Sculli  Wednesday, October 04, 2006 10:15 PM

You can use google to search for other answers

Custom Search

More Threads

• how to customize datagrid row based on data?
• DataGridView undo/redo
• datagridview dynamic build
• Cannot Create a oledb connection using wizard(s)
• Add a row to DataGridView
• Datagridview checkbox.checked event
• Trying to order by number in datagrid
• datagrid / variable problem
• How to select a single xml node from an xmldocument that has namespaces.
• System.Data.OleDb.OleDbException while querying access 2000