Solved

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

Posted on 2014-11-20
8
172 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

828 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