[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Repetitive Dataset Sorting

Posted on 2004-10-04
13
Medium Priority
?
272 Views
Last Modified: 2010-04-23
I have a Dataset that draws everything from one table.  I'll call this dataset_test.

In dataset_test is the table "TestTBL"

In the TestTBL table are the columns "ColFirst", "ColLast", and "ColID"

I have 4 combo boxes.  One sits on top, the other three are below.  These three lower ones all occupy the same space.  The options in the first combo box are:
First Name, Last Name, Employee ID

Depending on which is chosen, one of the three lower boxes is made visible (and the other two invisible).  These three are directly datasource and datamember'd to ColFirst, ColLast, and ColID in the TestTBL table in the dataset_test dataset.  However, each time one of the three options is chosen in the first combo box, I want the newly visible combo box to be sorted alphabetically, such that when last name is chosen, the last names appear in the lower combo box alphabetically (the default), when First Name is chosen, rather than they appear in the same order as the last names (due to the databindings), the dataset is reordered for first names, and the same for Employee IDs.

How do I do this?

JP
0
Comment
Question by:gleznov
[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
  • 7
  • 6
13 Comments
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12221592
Use a dataView to the table and you can set the Sort property according to the comboBoxes.
0
 

Author Comment

by:gleznov
ID: 12225564
any example code?

JP
0
 

Author Comment

by:gleznov
ID: 12225603
I made a dataview using the GUI controls and used the properties to set it to my table.  But  what code will cause it to sort by a certain column?

JP
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:eozz_2000
ID: 12226666
In sort property of the dataView, put something like this:

"Name DESC", to order by name in descending order
0
 

Author Comment

by:gleznov
ID: 12226708
OK, maybe step me through this whole process and give me some code?  I'm just not getting it.

JP
0
 

Author Comment

by:gleznov
ID: 12229557
Anybody?

JP
0
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12232523
In this example I have tree radio buttons: rbProductName, rbProductID and rbUnitsInStock, one datagrid, one dataAdapter (daProducts), and one dataSet (dsGeneral), I have worked with the Products table of Northwind (SQL Server):

Dim dv As DataView

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Me.daProducts.Fill(Me.dsGeneral.Products)
  dv = New DataView(Me.dsGeneral.Products)
  With dv
    .AllowDelete = False
    .AllowEdit = False
    .AllowNew = False
  End With
  Me.DataGrid1.DataSource = dv
End Sub

Private Sub rbProductName_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbProductName.CheckedChanged
  If Me.rbProductName.Checked Then
    Me.dv.Sort = "ProductName ASC"
  End If
End Sub

Private Sub rbProductID_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbProductID.CheckedChanged
  If Me.rbProductID.Checked Then
    Me.dv.Sort = "ProductID ASC"
  End If
End Sub

Private Sub rbUnitsInStock_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbUnitsInStock.CheckedChanged
  If Me.rbUnitsInStock.Checked Then
    Me.dv.Sort = "UnitsInStock ASC"
  End If
End Sub
0
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12232526
Sorry because I answer to late but I went to work, I hope this could help you :)
0
 

Author Comment

by:gleznov
ID: 12236511
I feel like I'm close now, but something's not clicking.  Here's what I've got:

In the open area where you define public variables, I have:

    Public dv As DataView


In my form_load routine I have:

        Dim da As New SqlClient.SqlDataAdapter("select * from Custodians order by last_name", SqlConnection1)
        da.Fill(DataSet_Cust1, "Custodians")

        ' Setup Dataview
        dv = New DataView(DataSet_Cust1.Custodians)
        With dv
            .AllowDelete = False
            .AllowEdit = False
            .AllowNew = False
        End With

        cmbSearch.Items.Add("First Name")
        cmbSearch.Items.Add("Last Name")
        cmbSearch.Items.Add("Employee ID")
        cmbSearch.SelectedIndex = 1
        cmbSearch.Refresh()


And finally, this sub is where I need the sorting done - when a different selection is made (First Name, Last Name, Employee ID), it shows the corresponding combo box.  

    Private Sub cmbSearch_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbSearch.SelectedValueChanged
        If cmbSearch.Text = "First Name" Then
            cmbFirst.Visible = True
            cmbLast.Visible = False
            cmbID.Visible = False
            SOption = 0
            dv.Sort = "First_Name"
        ElseIf cmbSearch.Text = "Last Name" Then
            cmbFirst.Visible = False
            cmbLast.Visible = True
            cmbID.Visible = False
            SOption = 1
            dv.Sort = "Last_Name"
        Else
            cmbFirst.Visible = False
            cmbLast.Visible = False
            cmbID.Visible = True
            SOption = 2
            dv.Sort = "Employee_ID"
        End If
    End Sub


Finally, I bound each of those combo boxes to their respective field in the dataview's custodians table.

Still, when I switch to first name or employee ID, nothing's different - they're still in the same order of last_name asc.  

JP
0
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12236898
let me try, I respond you at night :)
0
 
LVL 4

Accepted Solution

by:
eozz_2000 earned 2000 total points
ID: 12244719
Well, I tried with this code and works:

    Public dv As DataView

    Private Sub frmDataView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim da As New SqlClient.SqlDataAdapter("select * from Custodians order by LastName", Me.conn)
        Try
            da.Fill(Me.dsGeneral.Custodians)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        ' Setup Dataview
        dv = New DataView(Me.dsGeneral.Custodians)
        With dv
            .AllowDelete = False
            .AllowEdit = False
            .AllowNew = False
        End With
        Me.DataGrid1.DataSource = Me.dv

        cmbSearch.Items.Add("First Name")
        cmbSearch.Items.Add("Last Name")
        cmbSearch.Items.Add("Employee ID")
        cmbSearch.SelectedIndex = 1
        cmbSearch.Refresh()
    End Sub
 
Private Sub cmbSearch_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbSearch.SelectedValueChanged
  If cmbSearch.Text = "First Name" Then
    cmbFirst.Visible = True
    cmbLast.Visible = False
    cmbID.Visible = False
    'SOption = 0
    dv.Sort = "FirstName"
   ElseIf cmbSearch.Text = "Last Name" Then
     cmbFirst.Visible = False
     cmbLast.Visible = True
     cmbID.Visible = False
     'SOption = 1
     dv.Sort = "LastName"
   Else
     cmbFirst.Visible = False
     cmbLast.Visible = False
     cmbID.Visible = True
     'SOption = 2
     dv.Sort = "EmployeeID"
  End If
  Me.cmbFirst.DataSource = Me.dv
  Me.cmbFirst.DisplayMember = "FirstName"
  Me.cmbLast.DataSource = Me.dv
  Me.cmbLast.DisplayMember = "LastName"
  Me.cmbID.DataSource = Me.dv
  Me.cmbID.DisplayMember = "EmployeeID"
End Sub

My table is:

CREATE TABLE Custodians(
  FirstName NVARCHAR(20),
  LastName NVARCHAR(20),
  EmployeeID INT IDENTITY(1,1) PRIMARY KEY
)

And when I select a value in the cmbSearch the dataView is ordered like I want, and the values in the others combos is ordered too.

I hope this could help you :)
0
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12245264
Where are you? :)
0
 

Author Comment

by:gleznov
ID: 12247874
Thanks eozz.  Unfortunately this morning my project has been turned on its heels and I have to redo development in ASP.NET or VB.NET webforms.  Thanks for the help!

JP
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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 …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

649 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