Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Double click to sort a data grid 1

Status
Not open for further replies.

cruise95

Programmer
Oct 7, 2003
56
US
How do I go about sorting a column on a Dbl-Click instead of a single click in a datagrid?

I don't want to sort on a single click but on a double click.

I've got it working by re-querying the database if the user wants to sort ascending or descending, overwriting the dataset, creating another table style, and setting the dataset as the datagrid's datasource.

That seems like alot of work to go through. Is there some other way to use the datagrid's default sort method?

Thanx
 
The datagrid has a DoubleClick Event
Also the best way to sort data is to use a dataview. This way you only have to select the data once. Using a dataview you can specify the sort order and also add "asc" or "desc" for ascending/descending.



Sweep
...if it works, you know the rest..
Always remember that Google is your friend

curse.gif
 
Thanx again Sweep,

The dataView worked like you said...but I need to look into the whole thing again. My dataGrid gets its data from MS SQL and makes a call to it every 15 seconds. I need to do this without re-loading the dataset and the datagrid that is bound to it. In other words I do not want the user to be looking at some row and then 15 seconds later have the datagrid refreshed so that row is lost.

All the new records in MS SQL are appended to the datagrid using the dataset's .Merge method. Unfortunately the dataView does not have this capability.

The only that I can see is:
1. Re-query the database (with an Order By clause) if the user dbl-clicks on a column header.

Unfortunately, there could be up to 2,800 records in the database. So my way might take too long.


Any suggestions?
 
Why cant you just reapply the dataview, after fetching the new data?.

Layer.1 = data ops (data fetch). In here simply update your dataset.
Layer.2 = data display (include some logic so that you can always recall the last sort order for your dataview).

The only times to reapply the dataview are when 1) data changes or 2) by User action.



Sweep
...if it works, you know the rest..
Always remember that Google is your friend

curse.gif
 
That's another thing I was just thinking of...I will to try that

1. Use a dataSet to initially fetch the data.

[Loop]
2. When the user dbl-clicks on a column header, then convert the dataSet into a a dataView (for soting).
3. Immediately after the data is sorted, then convert this dataView back into a dataSet (so the Merge method will work 15 seconds later).
[/Loop]

Is this correct? Thanx Sweep


Can I ask you about the difference between a component class and a user control...or can you recomend a good link?

That is one thing that was on my list of things to research this morning: I'm using two component classes to make my customized dataGrid DLL. However, whenever I add this DLL to the toolBox an ugly default image of a gear shows up next to it. I have looked but cannot seem to change this image. Maybe I should be using a User Control instead.
 
No "conversions" needed
The dataset/datatable is the bottom layer.
Think of the dataview as a second layer which fits snugly onto the datatable/set simply as a means of display handling. It would be nice even when your data refreshed, that you kept the current dataview, in so much as the sort order remained the same as before the data refresh.

I like(or am used to) the Gear Image. Its obviously possible to customize it, but Ive never looked into it. Probably has something to do with Atrribute settings.

My understanding of component class, User Control.
Component class is based on a single Windows form control (eg MyLabel .... Inherits Label, MyTextBox....Inherits TextBox)
User Control is usually a composite control, made up of several component or base classes.


Sweep
...if it works, you know the rest..
Always remember that Google is your friend

curse.gif
 
I did some testing and that worked great!

Thanx for all your help Sweep. I'm still working on it (The SortDirection field is for all columns when it should be an array so that each column has its own sortDirection). But here's what I've got working so far if anyone wants to see it.


Code:
Public Class DataGrid
  ' This variable/property holds the current column that is selected
  Private _curRowSelected As Integer = -1
  Public Property CurRowSelected() As Integer
    Get
      Return _curRowSelected
    End Get
    Set(ByVal Value As Integer)
      _curRowSelected = Value
    End Set
  End Property

  Private _sortDirection As String = "ASC"
  Public Property SortDirection() As String
    Get
      Return _sortDirection
    End Get
    Set(ByVal Value As String)
      _sortDirection = Value
    End Set
  End Property

  
  Private Sub sortAscDesc()
    Dim myDataView As System.Data.DataView
    Dim myDataTable As System.Data.DataTable
    Dim columnName As String

    Try
      ' Get the DefaultViewManager of a datatable
      myDataTable = Me.DataSource
      myDataView = myDataTable.DefaultView

      ' Get name of column clicked on
      columnName = myDataTable.Columns(ColSelections(0)).ColumnName

      ' Sort criteria
      myDataView.Sort = columnName & " " & SortDirection


      ''' DataSet --> DataView '''
      Me.DataSource = myDataView

      ''' DataView --> DataSet '''
      Me.DataSource = myDataView.Table()

      If SortDirection = "ASC" Then
        SortDirection = "DESC"
      Else
        SortDirection = "ASC"
      End If


    Catch ex As Exception
      MsgBox("HEY IDIOT...What the hell are you doing?")
    End Try
  End Sub
End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top