?
Solved

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

Posted on 2014-11-20
8
Medium Priority
?
277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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 2000 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

Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Starting up a Project

765 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