Excluding specific rows from automatic sort in a .net Datagridview with datasource

I have a DataGridView with datasource, when I add a new row one of the fields is a combobox and is populated on the DataGridView_RowEnter event.

Private Sub DataGridView_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView.RowEnter
        If DataGridView.Rows(e.RowIndex).IsNewRow = True Then
            Dim Cell As DataGridViewComboBoxCell = New DataGridViewComboBoxCell
            Cell.DataSource = ds.Tables(3)
            Cell.ValueMember = "Nombre"
            Cell.DisplayMember = "Nombre"
            DataGridView(2, e.RowIndex) = Cell
            DataGridView(2, e.RowIndex).ReadOnly = False
        End If
    End Sub

Open in new window

when the automatic sort is selected (after clicking the colum title) and you add a new row when it loses the focus it sorts correctly but the combobox format stays in the last row which shows an error .

So my question is How can I exclude the new row(s) from the automatic sort?

I've tried to access the SortCompare event but the event is not raised.
jaime infantesrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JimFiveCommented:
I think what you'll need to do is save the primary key for the new row and then after the sort happens you can search for that row and make it current and call RowEnter (unless that event triggers automatically).  I'm not sure which event you'll need to use to get it after the sort/refresh, maybe Form_Current.
0
jaime infantesrAuthor Commented:
Thanks for your interest

When the the colum title is clicked the sort is automatically and constantly made so y actually have no simple access to an event in orther to change the sorting algorithim.
0
JimFiveCommented:
I understand that, but when you enter the new row and the Datagrid is sorted and repainted there should be some event firing.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

jaime infantesrAuthor Commented:
I actually checked for the available events in the datagrid by putting a break point in most of them. Maybe I am overlooking, do you have an idea where to look next,

thanks
0
JimFiveCommented:
Did you try any events from the containing Form?
0
JimFiveCommented:
The only other thing I can think of is overriding datagridview_RowLeave()
0
x77Commented:
When you use a DGV - Bound to a DataSource, automatic Sort is based on DataSource Sort Capabilities.

Especial mention for DataTable or DataView.
When you assign a DataTable as DataSource, the real DataSource is DataTable.DefaultView (DataView).
When you set the Sort Property on DataView, a Binary Tree is created on DataView based on Sort Property.

Now you add a new row to DataTable / DataView (This is a Detached Row), it becomes the last Row.
This Row is not on DataTable (is a temporary row - DataTable.NewRow)

Note that you can set any local Variable with the DataGridViewRow.DataBoundItem (DataRowView) and also get the Table row.

When Cell is Validated The Row is Added to DataTable and DataView and row becomes ordered on DataView.
This is reflected on DGV, now you need locate the added row on DGV.

Now You can use a For loop and locate the position for our saved Row on CurrencyManager or DataView.
When you Know the Position you Can set the CurrencyManager.Position to move to added row or the DataGridview.CurrentCell property.

If you do┬┤nt like this behaviour, use  BindingList(Of T) Where T is a Class.
BindingList only sorts when you Click the DGV Column- No Automatic Sort.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.