Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sorting a bound dataset when the datagridview is sorted

Posted on 2006-07-03
5
Medium Priority
?
569 Views
Last Modified: 2008-02-26
I have a dataset that is bound to a datagridview.  Not all of the columns in the dataset show in the datagridview (namely the ID value of some of the columns).  When the user double clicks on a datagridview row the program shows the information for the selected item.

When the datagridview is sorted by clicking on a column header the dataset isn't sorted and sometimes the wrong ID is used and thus the wrong information is shown.  How do I automatically have the dataset sort when the datagridview is sorted?

Here's some of my code for setting up the datagridview:

 Dim row As Data.DataRow

        ' Initialize a new instance of the data access base class
        objData = New DALBase

       
            ' Create columns
            objDataTable.Columns.Add("id")
            objDataTable.Columns.Add("contactID")
            objDataTable.Columns.Add("member")
            objDataTable.Columns.Add("property")
            objDataTable.Columns.Add("town")
            objDataTable.Columns.Add("state")
            objDataTable.Columns.Add("country")
            objDataTable.Columns.Add("acres")

            ' Get all customers in a DataReader object
            objData.SQL = "usp_SelectGroupProperties"
            objData.InitializeCommand()
            objData.OpenConnection()
            objData.DataReader = objData.Command.ExecuteReader

            ' See if there is any data before continuing
            If objData.DataReader.HasRows Then

                ' Process all rows
                While objData.DataReader.Read()
                    ' Add a new row to the dataset
                    row = objDataTable.NewRow
                    row("id") = objData.DataReader.Item("propertyID")
                    row("contactID") = objData.DataReader.Item("contactID")
                    row("member") = objData.DataReader.Item("contactName")
                    row("property") = objData.DataReader.Item("propertyName")
                    row("town") = objData.DataReader.Item("town")
                    row("state") = objData.DataReader.Item("state")
                    row("country") = objData.DataReader.Item("country")
                    row("acres") = objData.DataReader.Item("acres")
                    objDataTable.Rows.Add(row)
                End While
            End If

            objData.DataReader.Close()

            ' Make it so the datagrid doesn't automatically create the columns from the dataset table
            dgdMemberGroupProperty.AutoGenerateColumns = False
            dgdMemberGroupProperty.Columns.Clear()
            ' Assign the datasource for the data grid
            dgdMemberGroupProperty.DataSource = objDataTable
0
Comment
Question by:purfus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17033060
You can sort the DataTable:

objDataTable.DefaultView.Sort = "member ASC"

Bob
0
 
LVL 3

Author Comment

by:purfus
ID: 17036897
The sql that fills the dataset already orders the results by member.  What I want to happen is that when the user clicks on the datagridview header the dataset (or datatable, whichever one is more appropriate) would also be sorted to match how the datagridview is sorted.
0
 
LVL 34

Accepted Solution

by:
Sancler earned 1500 total points
ID: 17037185
To the best of my knowledge and belief, datatables cannot be "sorted" other than by an ORDER clause in the SQL statement that fills them.  As datasets do not themselves directly hold data - they only hold the datatables that hold the data - the same is true of them, too.

You can, however, interpose a DataView between a datatable and any control displaying data from that datatable.  You just set the DataView's .Table property to the datatable concerned, and make the control's .DataSource the DataView rather than the datatable.  Or you simply use the datatable's .DefaultView as in TheLearnedOne's example.  And, as he points out, you can sort DataViews.

Roger
0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 17040941
Hi

Pls check the following link

http://www.codeproject.com/aspnet/elphGrid.asp

hope it will help u

Cheers!!!
0
 
LVL 3

Author Comment

by:purfus
ID: 17055395
Thank you for the help.  I'll try these out.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

610 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