Re: DataGridView--after sorting



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


"ClayB" <clayb@xxxxxxxxxxxxxx> wrote in message
news:1174342639.980928.308010@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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.



.



Relevant Pages

  • Multiple Calendar Control on one page
    ... Protected WithEvents Calendar1 As System.Web.UI.WebControls.Calendar ... Private Sub Page_Init(ByVal sender As System.Object, ... Dim mo As String ...
    (microsoft.public.inetserver.asp.components)
  • Calendar Control issue.
    ... Protected WithEvents Calendar1 As System.Web.UI.WebControls.Calendar ... Private Sub Page_Init(ByVal sender As System.Object, ... Dim mo As String ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Strange FileWatch problem
    ... Private Sub Form1_Load(ByVal sender As System.Object, ... Dim Form2 As New Form2 ...
    (microsoft.public.dotnet.general)
  • Re: Strange FileWatch problem
    ... Private Sub Form1_Load(ByVal sender As System.Object, ... Dim Form2 As New Form2 ...
    (microsoft.public.dotnet.general)
  • Strange FileWatch problem
    ... Private Sub Form1_Load(ByVal sender As System.Object, ... Dim Form2 As New Form2 ...
    (microsoft.public.dotnet.general)