Home All Groups Group Topic Archive Search About

Datagrid colum sorting problem - URGENT

Author
24 Mar 2005 11:41 PM
Bill Nguyen
I found that rows in datagrid can be sorted by clicking on the column
header. However, this doesn't refresh the .currentindex
DataGrid1.CurrentCell.RowNumber

I need either to disable colum sorting programmatcially or somehow refresh
the DataGrid1.CurrentCell.RowNumber index so that it will reflect the
correct rownumber.



Any help is greatly appreciated!



Bill

Author
24 Mar 2005 11:54 PM
Ken Tucker [MVP]
Hi,

        Use the datatables default view to return right datarow.  Quick
example.


Dim ds As New DataSet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strConn As String

Dim strSQL As String

Dim da As OleDbDataAdapter

Dim conn As OleDbConnection

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

strConn &= "Data Source = Northwind.mdb;"

conn = New OleDbConnection(strConn)

da = New OleDbDataAdapter("Select * From Categories", conn)

da.Fill(ds, "Categories")

DataGrid1.DataSource = ds.Tables("Categories")

End Sub



Private Sub Form1_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.DoubleClick

Dim cm As CurrencyManager = CType(Me.BindingContext(DataGrid1.DataSource),
CurrencyManager)

Dim drv As DataRowView

drv = ds.Tables("Categories").DefaultView.Item(cm.Position)

MessageBox.Show(drv.Item("CategoryName").ToString)

End Sub


Disable sort on grid

Set the datagrids or tablestyle if you added one Allowsorting to false

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsdatagridtablestyleclassallowsortingtopic.asp


Ken

---------------------------
"Bill Nguyen" <billn_nospam_please@jaco.com> wrote in message
news:OEh95rMMFHA.2604@TK2MSFTNGP10.phx.gbl...
I found that rows in datagrid can be sorted by clicking on the column
header. However, this doesn't refresh the .currentindex
DataGrid1.CurrentCell.RowNumber

I need either to disable colum sorting programmatcially or somehow refresh
the DataGrid1.CurrentCell.RowNumber index so that it will reflect the
correct rownumber.



Any help is greatly appreciated!



Bill
Author
28 Mar 2005 8:09 PM
Bill Nguyen
Dear Ken;
I'm sorry for being a slow learner. I have the following functions/subs but
they seemed to be not working when I double-clicked on the Datagrid:

Private Sub DataGrid1_Navigate(ByVal sender As System.Object, ByVal ne As
System.Windows.Forms.NavigateEventArgs) Handles DataGrid1.Navigate

'txtBolID.Text = "hello!"

'

Dim s As String = "Selected rows:"

Dim o As Object

For Each o In GetSelectedRows(DataGrid1)

s += " " + o.ToString()

' MsgBox(s)

Next o







End Sub



Public Function GetSelectedRows(ByVal dg As DataGrid) As
System.Collections.ArrayList

'Dim alMvmt, alProd As String

Dim al As New ArrayList

Dim cm As CurrencyManager = Me.BindingContext(dg.DataSource, dg.DataMember)

Dim dv As DataView = CType(cm.List, DataView)

dv.AllowNew = False

'

Dim i As Integer

For i = 0 To dv.Count - 1

If dg.IsSelected(i) Then

al.Add(i)

alCustID = dv.Table.Rows(i).Item("custid")

alCustName = dv.Table.Rows(i).Item("custMPName")

' MsgBox(i & alCustID & " - " & alCustName)

End If

Next

Return al

'Return i



Show quoteHide quote
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:OPrBfzMMFHA.3512@TK2MSFTNGP15.phx.gbl...
> Hi,
>
>        Use the datatables default view to return right datarow.  Quick
> example.
>
>
> Dim ds As New DataSet
>
> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>
> Dim strConn As String
>
> Dim strSQL As String
>
> Dim da As OleDbDataAdapter
>
> Dim conn As OleDbConnection
>
> strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"
>
> strConn &= "Data Source = Northwind.mdb;"
>
> conn = New OleDbConnection(strConn)
>
> da = New OleDbDataAdapter("Select * From Categories", conn)
>
> da.Fill(ds, "Categories")
>
> DataGrid1.DataSource = ds.Tables("Categories")
>
> End Sub
>
>
>
> Private Sub Form1_DoubleClick(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles MyBase.DoubleClick
>
> Dim cm As CurrencyManager = CType(Me.BindingContext(DataGrid1.DataSource),
> CurrencyManager)
>
> Dim drv As DataRowView
>
> drv = ds.Tables("Categories").DefaultView.Item(cm.Position)
>
> MessageBox.Show(drv.Item("CategoryName").ToString)
>
> End Sub
>
>
> Disable sort on grid
>
> Set the datagrids or tablestyle if you added one Allowsorting to false
>
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsdatagridtablestyleclassallowsortingtopic.asp
>
>
> Ken
>
> ---------------------------
> "Bill Nguyen" <billn_nospam_please@jaco.com> wrote in message
> news:OEh95rMMFHA.2604@TK2MSFTNGP10.phx.gbl...
> I found that rows in datagrid can be sorted by clicking on the column
> header. However, this doesn't refresh the .currentindex
> DataGrid1.CurrentCell.RowNumber
>
> I need either to disable colum sorting programmatcially or somehow refresh
> the DataGrid1.CurrentCell.RowNumber index so that it will reflect the
> correct rownumber.
>
>
>
> Any help is greatly appreciated!
>
>
>
> Bill
>
>
>
>
>
Author
25 Mar 2005 5:22 AM
Bill Nguyen
Dear Ken;

Thanks for the tip.
I was able to set allowSortingg to False. However, still want to be able to
set allowsorting to True and get the correct rowid to display the record
detail on the right portion of the form.
The problem I seemed to have is that when you clicck on the column header to
sort ASC or DESC, the orgirinal rowid won't change to reflect the new sort
order. Will the CurrencyManager in your sample resolve this issue?
Thanks again

Bill

Show quoteHide quote
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:OPrBfzMMFHA.3512@TK2MSFTNGP15.phx.gbl...
> Hi,
>
>        Use the datatables default view to return right datarow.  Quick
> example.
>
>
> Dim ds As New DataSet
>
> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>
> Dim strConn As String
>
> Dim strSQL As String
>
> Dim da As OleDbDataAdapter
>
> Dim conn As OleDbConnection
>
> strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"
>
> strConn &= "Data Source = Northwind.mdb;"
>
> conn = New OleDbConnection(strConn)
>
> da = New OleDbDataAdapter("Select * From Categories", conn)
>
> da.Fill(ds, "Categories")
>
> DataGrid1.DataSource = ds.Tables("Categories")
>
> End Sub
>
>
>
> Private Sub Form1_DoubleClick(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles MyBase.DoubleClick
>
> Dim cm As CurrencyManager = CType(Me.BindingContext(DataGrid1.DataSource),
> CurrencyManager)
>
> Dim drv As DataRowView
>
> drv = ds.Tables("Categories").DefaultView.Item(cm.Position)
>
> MessageBox.Show(drv.Item("CategoryName").ToString)
>
> End Sub
>
>
> Disable sort on grid
>
> Set the datagrids or tablestyle if you added one Allowsorting to false
>
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsdatagridtablestyleclassallowsortingtopic.asp
>
>
> Ken
>
> ---------------------------
> "Bill Nguyen" <billn_nospam_please@jaco.com> wrote in message
> news:OEh95rMMFHA.2604@TK2MSFTNGP10.phx.gbl...
> I found that rows in datagrid can be sorted by clicking on the column
> header. However, this doesn't refresh the .currentindex
> DataGrid1.CurrentCell.RowNumber
>
> I need either to disable colum sorting programmatcially or somehow refresh
> the DataGrid1.CurrentCell.RowNumber index so that it will reflect the
> correct rownumber.
>
>
>
> Any help is greatly appreciated!
>
>
>
> Bill
>
>
>
>
>
Author
25 Mar 2005 7:56 AM
Cor Ligthert
Bill,

I don't use the

DataGrid1.CurrentCell.RowNumber

However that

CType(Me.BindingContext(DataGrid1.DataSource),
CurrencyManager).Position

That you see in the sample from Ken.

I hope this helps,

Cor