Datagrid colum sorting problem - URGENT

B

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
 
K

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/d...sdatagridtablestyleclassallowsortingtopic.asp


Ken

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

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
 
C

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
 
B

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
 

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