Solved

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

Posted on 2014-11-20
8
131 Views
Last Modified: 2016-06-23
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.
0
Comment
Question by:jaime infante
  • 4
  • 2
8 Comments
 
LVL 15

Expert Comment

by:JimFive
ID: 40480920
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
 

Author Comment

by:jaime infante
ID: 40481213
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
 
LVL 15

Expert Comment

by:JimFive
ID: 40481780
I understand that, but when you enter the new row and the Datagrid is sorted and repainted there should be some event firing.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:jaime infante
ID: 40482811
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
 
LVL 15

Expert Comment

by:JimFive
ID: 40483207
Did you try any events from the containing Form?
0
 
LVL 15

Expert Comment

by:JimFive
ID: 40483221
The only other thing I can think of is overriding datagridview_RowLeave()
0
 
LVL 15

Accepted Solution

by:
x77 earned 500 total points
ID: 40558075
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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now