|
-
Jul 9th, 2011, 10:18 AM
#1
Thread Starter
Junior Member
[RESOLVED] clicking column header to sort [DGV, BindingSource & DataTable]
Hi all,
I have a VB 2008 connected to my localhost SQL using DataGridview, BindingSource and DataTable.
All works fine, i can add, edit and delete the row and the SQL database is also affected.
However, there is one thing that bugs me, that is sorting.
Problem
My table in this database is still quite empty, it only has 6 rows
However, if I spam-click the column header, the sorting seems taking some time to sort, so that, the sorting result does not follow my mouse spam-clickings
Note:
Spam Click = Clicking the mouse button in high frequency, something like click click click click click click click click click, etc
I then make a debug button to check this
Code:
Variables:
Dim DebugInt As Integer = 0
Dim bs As New BindingSource
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If DebugInt = 0 Then
bs.Sort = "name desc"
DebugInt = 1
Else
bs.Sort = "name asc"
DebugInt = 0
End If
End Sub
If I spam-click this debug button, the sorting is fast and follows my clickings.
Why clicking my debug button gives better and faster result than clicking the column header to sort?
Why is this happening?
I am not really sure if my explanation above is clear enough.
Please help me solving this problem so that spam-clicking the column header sorts the table in a quick time
I am looking forward to hearing from you
Regards,
ryonn
-
Jul 9th, 2011, 09:55 PM
#2
Re: clicking column header to sort [DGV, BindingSource & DataTable]
 Originally Posted by ryonn
Hi all,
I have a VB 2008 connected to my localhost SQL using DataGridview, BindingSource and DataTable.
All works fine, i can add, edit and delete the row and the SQL database is also affected.
However, there is one thing that bugs me, that is sorting.
Problem
My table in this database is still quite empty, it only has 6 rows
However, if I spam-click the column header, the sorting seems taking some time to sort, so that, the sorting result does not follow my mouse spam-clickings
Note:
Spam Click = Clicking the mouse button in high frequency, something like click click click click click click click click click, etc
I then make a debug button to check this
Code:
Variables:
Dim DebugInt As Integer = 0
Dim bs As New BindingSource
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If DebugInt = 0 Then
bs.Sort = "name desc"
DebugInt = 1
Else
bs.Sort = "name asc"
DebugInt = 0
End If
End Sub
If I spam-click this debug button, the sorting is fast and follows my clickings.
Why clicking my debug button gives better and faster result than clicking the column header to sort?
Why is this happening?
I am not really sure if my explanation above is clear enough.
Please help me solving this problem so that spam-clicking the column header sorts the table in a quick time
I am looking forward to hearing from you
Regards,
ryonn
Normal users do not as you call it spam click, besides it is impossible to see results of said sort that quickly with the naked eye. If there is another intent behind spam click I do not see it.
-
Jul 10th, 2011, 06:28 PM
#3
Thread Starter
Junior Member
Re: clicking column header to sort [DGV, BindingSource & DataTable]
Hi Kevin,
Thanks for the reply.
This program is going to be privately used by me, and I might require a lot of sorting in a short time.
Is there any way to 'manipulate' the coding so that if the column header is clicked, it is the BindingSource that is sorted, not the DataGridView?
I am looking forward to hearing from you.
Regards,
R
-
Jul 10th, 2011, 09:38 PM
#4
Re: clicking column header to sort [DGV, BindingSource & DataTable]
 Originally Posted by ryonn
Hi Kevin,
Is there any way to 'manipulate' the coding so that if the column header is clicked, it is the BindingSource that is sorted, not the DataGridView?
In the following example bsCustomers DataSource is a DataTable. There is no initial sort going in. DataGridView1 columns are auto generated when assigning the DataSource to the grid.
Put each of the columns into programmatic sort mode.
Code:
Private Sub dataGridView1_DataBindingComplete( _
ByVal sender As Object, _
ByVal e As DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
For Each Column As DataGridViewColumn In DataGridView1.Columns
Column.SortMode = DataGridViewColumnSortMode.Programmatic
Next Column
End Sub
Sort
Code:
Private Sub DataGridView1_ColumnHeaderMouseClick( _
ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs) _
Handles DataGridView1.ColumnHeaderMouseClick
Dim ColumnName As String = DataGridView1.Columns(e.ColumnIndex).Name
Dim SortDirection As ListSortDirection
If bsCustomers.Sort = "" Then
SortDirection = ListSortDirection.Ascending
bsCustomers.Sort = ColumnName & " ASC"
Else
If bsCustomers.Sort.Contains("ASC") Then
bsCustomers.Sort = ColumnName & " DESC"
SortDirection = ListSortDirection.Descending
Else
bsCustomers.Sort = ColumnName & " ASC"
SortDirection = ListSortDirection.Ascending
End If
End If
DataGridView1.Columns(e.ColumnIndex).HeaderCell.SortGlyphDirection = _
If(SortDirection = ListSortDirection.Ascending, _
SortOrder.Ascending, _
SortOrder.Descending _
)
End Sub
-
Jul 11th, 2011, 01:28 AM
#5
Thread Starter
Junior Member
Re: clicking column header to sort [DGV, BindingSource & DataTable]
Hi Kevin,
Thanks Kevin. The speed is still the same.
However, I use most part of your code to apply this to CellMouseUp and it works great!
Code:
Private Sub DataGridView_CellMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView.CellMouseUp
If e.RowIndex <= -1 Then ' prevents error on column header click
Dim ColumnName As String = DataGridView.Columns(e.ColumnIndex).Name
Dim SortDirection As ListSortDirection
If bs.Sort = "" Then
SortDirection = ListSortDirection.Ascending
bs.Sort = ColumnName & " ASC"
Else
If bs.Sort.Contains("ASC") Then
bs.Sort = ColumnName & " DESC"
SortDirection = ListSortDirection.Descending
Else
bs.Sort = ColumnName & " ASC"
SortDirection = ListSortDirection.Ascending
End If
End If
End If
End Sub
Thanks.
+1 rep for you =)
Thread resolved
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|