R
R
Roman Sokolov2014-07-09 08:42:26
C++ / C#
Roman Sokolov, 2014-07-09 08:42:26

How to prevent automatic sorting when adding a record in DataGridView C#?

On the form, a DataGridView and a ComboBox bound to one of the BindingSource fields that is not displayed in the DataGridView. It looks like this:

groupheadersBindingSource.Sort = "IsTrainer DESC, HeaderTitle ASC";

sportTypeComboBox.DisplayMember = "SportTitle";
sportTypeComboBox.ValueMember = "SportID";
sportTypeComboBox.DataSource = sportTypesBindingSource;
sportTypeComboBox.DataBindings.Add("SelectedValue", groupheadersBindingSource, "SportID");

When adding an entry, the main fields are set directly in the grid, and the SportType is selected from the drop-down list. The problem is that with the current sorting options, when it comes to choosing a sport, the list in the grid is automatically sorted and the index flies off the edited entry.
Question: is it possible to somehow prevent updating the sort when editing a record?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Sokolov, 2014-07-09
@jimquery

So far I have done Workaround: We declare
two variables:

DataRowView editingRowView   = null;
bool addingRow = false;

Then we set in the designer:
groupHeadersBindingNavigator.AddNewItem = null;
dataGridView.AllowUserToAddRows = false;

Next, when you click the button, add an entry in the navigator:
editingRowView = (DataRowView)groupheadersBindingSource.AddNew();
addingRow = true;

And when clicked:
void sportTypeComboBox_Click ..
{
if ((editingRowView != null) && (addingRow))
            {
                groupheadersBindingSource.Position = groupheadersBindingSource.Find("HeaderID", editingRowView.Row["HeaderID"]);
                addingRow = false;
                editingRowView = null;
            }
}

Here is such a crutch turned out. At the same time, automatic sorting remains, but the pointer is restored from the current position.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question