SandyB Posted October 31, 2003 Posted October 31, 2003 I have 3 comboboxes on a form that show 3 separate columns from the same table. The sql behind them sorts on the column displayed in combobox1. I want the user to be able to re-sort so that the column displayed in combobox2 is sorted. After they select an item, other data is retrieved. I tried creating 3 separate dataadapters, but can't have multiple instances of the same table in the dataset. Does anyone have any suggestions for solving this? Thanks Quote
*Experts* jfackler Posted October 31, 2003 *Experts* Posted October 31, 2003 Create dataviews and sort the dataviews, then bind the comboboxes to the dataviews. The snip below should give you the idea. With the selectedindex change on the cmbCustomer, a combobox is populated with House Plan names. With the selection of a House Plan, a combobox is populated with Elevations. Private Sub cmbCustomer_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbCustomer.SelectedIndexChanged intjob = cmbCustomer.SelectedIndex dvHouse = DsHouse1.Tables("House").DefaultView dvHouse.RowFilter = "BuilderID = " & CStr(DsBuilder1.Tables("Builder").Rows(intjob).Item("BuilderID")) cmbHouse.DataSource = dvHouse cmbHouse.DisplayMember = "HouseName" End Sub Private Sub cmbHouse_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbHouse.SelectedIndexChanged Dim inthouse As Integer Dim dvhouseforel As DataView dvhouseforel = DsHouse1.Tables("House").DefaultView dvhouseforel.Sort = "HouseName" inthouse = dvhouseforel.Find(cmbHouse.Text) Dim dvElevation As DataView dvElevation = DsElevation1.Tables("Elevation").DefaultView dvElevation.RowFilter = "HouseID = " & CStr(dvhouseforel(inthouse).Item("HouseID")) Debug.WriteLine("HouseId is " & CStr(dvhouseforel(inthouse).Item("HouseID"))) If dvElevation.Count < 1 Then cmbElevation.Text = "No Elevation Information Available" Else : cmbElevation.DataSource = dvElevation cmbElevation.DisplayMember = "Name" End If End Sub Jon Quote
SandyB Posted October 31, 2003 Author Posted October 31, 2003 That's not quite what I was trying to do. The 3 combo boxes represent 3 different columns in a table - same row. I have Name, ID, Alias. I want the user to be able to select by any of these 3 criteria to populate a datagrid that pulls data from a related table in the dataset. In order to use the drop down feature of cboName, cboID, or cboAlias, they all have to be sorted by their respective column. Quote
Moderators Robby Posted October 31, 2003 Moderators Posted October 31, 2003 If each of the three combos should be sorted with disregard for other controls then I would pull in a seperate table for each one, binding each control with their respective Datatable. The Select statement for each should be taylored for theri particular needs, ie. cboName would only Select name and ID, cboAlias would be Alias and ID, cboID would only select ID. I rambled on and on, I'm not sure if you got it. :) Quote Visit...Bassic Software
SandyB Posted October 31, 2003 Author Posted October 31, 2003 I can't have a table in the dataset multiple times. That's part of my problem. Quote
*Experts* jfackler Posted November 1, 2003 *Experts* Posted November 1, 2003 Still not clear exactly what you want to do, but the attached accesses the Northwind database (you'll need to change the connection in the sqlconnection), three comboboxes on three columns (employeeId, FirstName, LastName) and a datagrid that sorts when a column is clicked. The currencymanager controls which record you're on, and selecting a row in the datagrid, or changing the selected index in any of the comboboxes moves each controls position. Take a look. I'm playing with the datagrid, it's gotten a lot of harsh criticism on this forum. I think the criticism is more a misinformation issue than anything else. This is pretty basic....not real difficult and that being the case probably doesn't address your concerns from what your post implies. Sounds like your looking for something more complex....let me know, I want to continue these exercises with the datagrid. Jonform1.vb Quote
*Experts* jfackler Posted November 1, 2003 *Experts* Posted November 1, 2003 Adding this little piece, lets you sort by clicking the column header in the datagrid. Yes I know the datagrid is capable of sorting simply by clicking the column header the difference here is the binding context stays with the value in the selected row of the column you sort. Private Sub DataGrid1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseDown Dim pt As System.Drawing.Point = New Point(e.X, e.Y) Dim hti As DataGrid.HitTestInfo = DataGrid1.HitTest(pt) Dim datav As DataView Dim sort As String Dim int As Integer = 0 Dim k As Object If hti.Type = Windows.Forms.DataGrid.HitTestType.ColumnHeader Then k = DataGrid1.Item(DataGrid1.CurrentRowIndex, hti.Column()) Debug.WriteLine("k is " & k) DataSet11.Tables("Employees").DefaultView.Sort = DataSet11.Tables("Employees").Columns.Item(hti.Column).ColumnName.ToString Debug.WriteLine("sort column is " & DataSet11.Tables("Employees").Columns.Item(hti.Column).ColumnName.ToString) int = DataSet11.Tables("Employees").DefaultView.Find(k) Debug.WriteLine("int is " & int) Me.BindingContext(ds).Position = int DataGrid1.AllowSorting = False End If End Sub Jon Quote
SandyB Posted November 3, 2003 Author Posted November 3, 2003 I tried your code in the nwind example. It doesn't seem to work the first time that you click on a header. It looks like it is supposed to sort first, then find k, but it doesn't sort on the first click, only subsequent clicks. Did you find the same thing? Please post your response. I'm new at this and want to learn. In my code, I wanted the 3 comboboxes to always be sorted by their respective columns. I solved it by creating 3 separate dataadapters with 3 separate datasets. I set the valuemember property of all 3 to the same column so that when the user selects a row in one, I set the valuemember of the other 2 and they scroll to their correct locations. This works well. Thanks for your input. Quote
Moderators Robby Posted November 3, 2003 Moderators Posted November 3, 2003 Hmm, isn't that similiar to Robbys' suggestion? Quote Visit...Bassic Software
SandyB Posted November 3, 2003 Author Posted November 3, 2003 Yes - you are right. I didn't understand your reply at the time. I'm really new at this. Thanks for your help. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.