Solved

How to sort a dataview programmatically

Posted on 2010-09-17
20
396 Views
Last Modified: 2013-11-07
Hi,
Need to sort a datagridview on it dataview property.The program should find out its previous sort method and decide automatically whether to sort the column desc or asc .
Any way of doing this?


Cheers
0
Comment
Question by:RIAS
20 Comments
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 33702453
You can check this link and will explain everything
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.sortedcolumn.aspx

. One thing that you must do is chechk the event ColumnHeaderMuouseClick if you want to sort on click over the headr of the column and apply this code right there with a few modifications
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33702486
What do you mean by "Need to sort a datagridview on it dataview property"?
0
 

Author Comment

by:RIAS
ID: 33702511
What do you mean by "Need to sort a datagridview on it dataview property"?
 Sort on the datatable.defaultview
0
 

Author Comment

by:RIAS
ID: 33702522
Hi k-designersDate,
The code I am already using right now but it sorts on datatable and not on default view of datatable.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33702903
try this

Dim dt As DataTable

Dim myDataView As DataView



dt.Load(dataReader)

myDataView = dt.DefaultView

myDataView.Sort = "mycolumn DESC"

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33703137
simply you can do
myTable.DefaultView.Sort = "TableColumn1 ASC , TableColumn2 DESC"
One order attribute effects only one column..
Hope It Helps..
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33711577
Also you can Sort data Via Direct Sql Statement Before  you set it as a datasource in datagridview....
SELECT........WHERE .......ORDER BY
0
 

Author Comment

by:RIAS
ID: 33712235
Hi jtoutou,
Thanks for the suggestions.But this sorting of the textboxcolumn based on the value of the cell.It is not a result of a sql query.
Any suggestions on how to find the sort direction of the dataview programmatically?
 
Cheers
 
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33712245
0
 

Author Comment

by:RIAS
ID: 33714574
Cheers mate...look like a very useful solution.Will try and get back asap
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:RIAS
ID: 33714638
Thanks jtoutou but as I had mentioned my textboxcolumn in grid is not databound.. it is not a query result.I have added it in the datatable by
 

Dim bs As New BindingSource
dtTemp = bs.DataSource
dtTemp.Columns.Add("Selection", GetType(Object))
dtTemp.Columns("Selection").SetOrdinal(0)
bs.DataSource = dtTemp
Cheers mate really appreciate your help
 
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33714806
so you have mixed bound and non databound columns ..so why do you need dataview sorting in a non databound column..?If you bind the DataGridView to a DataTable, the DataGridView calls the
sorting function provided by the default DataView of the DataTable to sort
the data shown in it.

0
 

Author Comment

by:RIAS
ID: 33715300
The problem is it sorts between 3 values dbnull,true and false I ned to sort only between true and false value
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33715634
why don't you set a default value for the boolean column?Checkbox should always have a 0 or a 1(Yes/No) Null in this case cause the CheckBox to an Unknown State.

0
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 500 total points
ID: 33715717
after populating your datagridview and before sorting it place this code ..and so you have only true and false
 For i as integer  = 0 To Me.DataGridView1.Rows.Count - 1

            If IsDBNull(Me.DataGridView1.Rows(i).Cells("YourCheckboxColumn").Value) Or Me.DataGridView1.Rows(i).Cells("YourCheckboxColumn").Value Is Nothing Then

                DataGridView1.Rows(i).Cells("YourCheckboxColumn").Value = False

            End If

        Next

Open in new window

0
 

Author Comment

by:RIAS
ID: 33716247
jtoutou,
I have rows something like 163000 .Imagine the load speed if I have to loop through all rows .
Cheers
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33717507
you don't have to load all rows at once...somehow you should create a filter....and so the loop has to do in the datagridview not in the datatable...
0
 

Author Comment

by:RIAS
ID: 33717736
somehow you should create a filter?I didnt understand this bit .Going little mad and hope I am not driving you mad too.
Cheers mate
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33717774
ok...let is start again ....you are populating a datagridview with 163000 rows?
what i am saying that you do not have to populate all these rows...your data tabale contains these number of rows...you should populate the datagridview with rows deppending on user's request..
so because your checkboxcolumn is a custom column and not databound you dont have a problem about looping through the datagridview ....:)
0
 

Author Comment

by:RIAS
ID: 33717811
Hi,
Oh understood now.But should populate the datagridview with rows deppending on user's request..is not possible here as it ia grid which loads all records in a table.i am working on a conversion project vb 6 to vb.net .So can't change the specs now the grid needs to be populated with all members in a table.
Cheers mate

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now