DataGridView--after sorting

K

Kevin S Gallagher

Just learned that after clicking on a DataGridView column (using the built
in sort) that it sorts fine but what was the currently selected row is not
afterwards. Any ideas on how to resolve this issue so that the original row
is still selected.

Thanks
Kevin
 
K

Kevin S Gallagher

Just noticed I should have posted to the windowsforms control forum and will
post there if I don't resolve this issue later today.
My apologies for posting in the wrong forum but as mentioned above will wait
before "cross-posting".
 
C

ClayB

Here is one way you can try to do this. Remember the current item in
the grid's MouseDown event. Then in the grid's Sorted event, reset
this current item. Here is a snippet that does this by remembering the
value of the primarykey column in MouseDown and then resetting this in
the Sorted event. It assumes you have a primary key column named
pkColumn and the DataGridView.DataSource is a this.bindingSource1
object.


private object pkValue = null;

void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
pkValue = dataGridView1["pkColumn",
dataGridView1.CurrentCell.RowIndex].Value;
}
void dataGridView1_Sorted(object sender, EventArgs e)
{
int rowIndex = this.bSrc.Find("pkColumn", pkValue);
this.bindingSource1.CurrencyManager.Position =
rowIndex;
}
===================
Clay Burch
Syncfusion, Inc.
 
S

Scott M.

You'll probably also find that if you sort and then change to a different
page of data, the new page will come in, but you will lose your sort.

The solution to this and your first question is to store the current row
number and the current sort criteria, so they can be re-applied after a user
action (such as changing pages).
 
K

Kevin S Gallagher

First off thanks for the suggestions.

I tried implementing your code but could not get it to work which I am sure
is something I am doing wrong. Below is my code and wondered if anyone could
tell me what I might be doing wrong? Thanks Clay and anyone else!!!!

Kevin

This is the shortest code I could come up with. The first field is auto
followed by usual fields i.e. first/lastname etc. and no exception handling
to keep the code clear.

Imports System.Data.OleDb
Public Class Form1
Dim BindingSource1 As Windows.Forms.BindingSource = New BindingSource
Dim pkValue As Object
Property FieldValue() As Object
Get
Return pkValue
End Get
Set(ByVal value As Object)
pkValue = value
End Set
End Property
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim strConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=example.mdb;"
Dim TheConnection As OleDbConnection = New
OleDbConnection(strConnection)
Dim da As New OleDbDataAdapter("SELECT * FROM Customers ORDER BY id",
TheConnection)
Dim dt As DataTable

dt = New DataTable
da.Fill(dt)
DataGridView1.DataSource = dt
BindingSource1.DataSource = dt
End Sub
Private Sub DataGridView1_MouseClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick
With DataGridView1
FieldValue = .Item(0, .CurrentRow.Index).Value ' Get ID field value
End With
End Sub
Private Sub DataGridView1_Sorted(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGridView1.Sorted
BindingSource1.CurrencyManager.Position = BindingSource1.Find("ID",
DirectCast(FieldValue, Integer))
End Sub
Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdClose.Click
Close()
End Sub
End Class
 
K

Kevin S Gallagher

Thanks for the information Scott!!!!

Scott M. said:
You'll probably also find that if you sort and then change to a different
page of data, the new page will come in, but you will lose your sort.

The solution to this and your first question is to store the current row
number and the current sort criteria, so they can be re-applied after a
user action (such as changing pages).
 
K

Kevin S Gallagher

Okay with Clay's assistance I came up with the following which works but not
sure if there is slimming down that could be done. If you have any
suggestions please post them. Otherwise the credit goes to Clay on this.

Imports System.Data.OleDb
Public Class Form1

Dim BindingSource1 As Windows.Forms.BindingSource = New BindingSource
Dim TheCurrencyManager As CurrencyManager

Dim pkValue As Object
Property FieldValue() As Object
Get
Return pkValue
End Get
Set(ByVal value As Object)
pkValue = value
End Set
End Property
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim strConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=example.mdb;"
Dim TheConnection As OleDbConnection = New
OleDbConnection(strConnection)
Dim da As New OleDbDataAdapter("SELECT * FROM Customers ORDER BY id",
TheConnection)
Dim dv As DataView
Dim ds As New DataSet


da.Fill(ds, "customers")
dv = New DataView(ds.Tables("customers"))
TheCurrencyManager = CType(Me.BindingContext(dv), CurrencyManager)
DataGridView1.DataSource = dv
BindingSource1.DataSource = dv
End Sub
Private Sub DataGridView1_MouseClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick
With DataGridView1
FieldValue = .Item(0, .CurrentRow.Index).Value ' Get ID field value
End With
End Sub
Private Sub DataGridView1_Sorted(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGridView1.Sorted
TheCurrencyManager.Position = BindingSource1.Find("ID",
DirectCast(FieldValue, Integer))
End Sub
Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdClose.Click
Close()
End Sub
End Class
 
C

ClayB

Try changing

DataGridView1.DataSource = dt
BindingSource1.DataSource = dt

to

BindingSource1.DataSource = dt
DataGridView1.DataSource = BindingSource1

==================
Clay Burch
 
K

Kevin S Gallagher

Thanks I will, but coffee first :)
Will post back my results.
Thanks again for your assistance!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top