Combox Confusion

  • Thread starter Thread starter Greg McKnight
  • Start date Start date
G

Greg McKnight

This may be the second time this post is showing, I accidentally shut
down my browser.
Sorry if it is a repeat.

I am a newbie to VB.net, so please forgive me if these are simple
questions.

I'm using the Northwind DB to teach myself. I used the data form wizard
to create a form with a dataset containing the
'Orders' and 'Order Detail' tables. The 'Orders' fields are displayed
in textboxes and the 'Detail' fields are in a
datagrid.

I'm trying to replace the 'ShipVia' textbox with a combobox that will
display the appropriate values from the 'Orders'
table when scrolling through records, but will return a list of values
from the 'Shippers' table when the dropdown is
clicked.
---
Here are the two problems I'm having:

1) The 'ShipVia' field in the 'Orders' table is actually based upon a
SQL query which returns a 'ShipID' in the first
column and a 'CompanyName' in the second column. How do I bind the
textbox to display the 'CompanyName' on the form?

2) How do I set the dropdown to return values from the 'CompanyName'
field in the 'Shippers' table, while actually
storing the 'ShipperID' value?

Any help will be greatly appreciated. I'm goin nuts here.
Thanks!!!



Greg M
 
1) The 'ShipVia' field in the 'Orders' table is actually based upon a
SQL query which returns a 'ShipID' in the first
column and a 'CompanyName' in the second column. How do I bind the
textbox to display the 'CompanyName' on the form?

Dim a As New DataSet()

Dim b As IEnumerator = a.Tables(0).Columns.GetEnumerator()

Dim combo As New ComboBox()

While b.MoveNext

Dim row As DataRow = b.Current

Dim columnIndex As Integer = 1 'the row you companyname is in

combo.Items.Add(row.Item(columnIndex))

End While

'This sets it in the combobox, but you should get the idea

2) How do I set the dropdown to return values from the 'CompanyName'
field in the 'Shippers' table, while actually
storing the 'ShipperID' value?

'when you press the combobox then you can just loop through your dataset to
get the matching ID


Hope that it helps
Regards Richard
 
Hi Greg,

The combobox is a great control. However it is that overloaded in my opinion
with functions, that it has in some places some unwanted behaviour and in
some places real bugs.

However basicly what you do to set the combobox to get values is adding
those values to the items one by one or setting a datasource to it and tell
what is the displaymember and what is the valuemember.

What is the way you want to go from the last two?

Cor
 
Hello Cor-

I would like to set a datasource to the combobox.
Could you help me with this?

Thanks!!

Greg M
 
Hello Richard-

I tried to copy the statement below and paste it into the code that runs
when the form loads.

At first I got this message:
An unhandled exception of type
'System.IndexOutOfRangeException' occurred in
system.data.dll

Additional information: Cannot find table 0.

So I replaced the 0 in the Dim b as IEnumerator line with
"Orders" and I got this message:

An unhandled exception of type
'System.NullReferenceException' occurred in
NorthwindTestEnvironment.exe

Additional information: Object reference not set to an
instance of an object.

I'm really new at this, so please forgive me if I'm asking basic
questions. I guess my new questions are:

1)should I have pasted this in the form load section?
2)which parts of the code block do I need to add my own variables (ie
should I set the Dataset in your dim A line to the name of my dataset?)

Thanks alot for responding and hopefully forgiving my lack of exposure
to this stuff (thus far)!!!

Greg M
 
Hi Greg,

Mad had a problem and I did not know if he had integrated his combobox in
his datagrid.
So I made a sample.

I hope it is also something for you?

Cor

\\\needs a datagrid and 2 buttons on a form (you can paste it in completly)
'Used for the comboboxcolumn is a modified sample from
'syncfusion
'To start create ds and cancel, than a start dataset
'will be created
Dim ds As New DataSet("Test")
Private Sub Form1_Load(ByVal sender As Object, ByVal e _
As System.EventArgs) Handles MyBase.Load
Me.Button1.Text = "Read/Create ds"
Me.Button2.Text = "Write ds"
End Sub
Private Sub FillGrid()
Dim dv As New DataView(ds.Tables(0))
dv.AllowNew = False
DataGrid1.DataSource = dv
Dim ts As New DataGridTableStyle
ts.MappingName = "Names"
Dim textCol As New DataGridTextBoxColumn
textCol.MappingName = "IdName"
textCol.HeaderText = "Id"
textCol.Width = 20
ts.GridColumnStyles.Add(textCol)
textCol = New DataGridTextBoxColumn
textCol.MappingName = "Name"
textCol.HeaderText = "Name"
textCol.Width = 120
ts.GridColumnStyles.Add(textCol)
Dim cmbTxtCol As New DataGridComboBoxColumn
cmbTxtCol.MappingName = "Country"
cmbTxtCol.HeaderText = "Countries"
cmbTxtCol.Width = 100
ts.GridColumnStyles.Add(cmbTxtCol)
ts.PreferredRowHeight = (cmbTxtCol.ColumnComboBox.Height + 3)
cmbTxtCol.ColumnComboBox.DataSource = ds.Tables(1)
cmbTxtCol.ColumnComboBox.DisplayMember = "Country"
cmbTxtCol.ColumnComboBox.ValueMember = "IdCountry"
cmbTxtCol.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList
DataGrid1.TableStyles.Add(ts)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim of As New SaveFileDialog
If of.ShowDialog = DialogResult.OK Then
ds.WriteXml(of.FileName)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim fo As New OpenFileDialog
If fo.ShowDialog() = DialogResult.OK Then
ds.ReadXml(fo.FileName)
Else
Dim dtName As New DataTable("Names")
Dim dcIdName As New DataColumn("IdName")
Dim dcName As New DataColumn("Name")
Dim dcCountryN As New DataColumn("Country")
dtName.Columns.Add(dcIdName)
dtName.Columns.Add(dcName)
dtName.Columns.Add(dcCountryN)
ds.Tables.Add(dtName)
For i As Integer = 1 To 5
Dim dr As DataRow = dtName.NewRow
dr(0) = i.ToString
dtName.Rows.Add(dr)
Next
dtName.Rows(0)(1) = "Herfried K. Wagner"
dtName.Rows(1)(1) = "Armin Zingler"
dtName.Rows(2)(1) = "Ken Tucker"
dtName.Rows(3)(1) = "CJ Taylor"
dtName.Rows(4)(1) = "Cor Ligthert"
dtName.Rows(0)(2) = "Austria(EU)"
dtName.Rows(1)(2) = "Germany(EU)"
dtName.Rows(2)(2) = "Georgia(US)"
dtName.Rows(3)(2) = "Other(US)"
dtName.Rows(4)(2) = "Holland(EU)"
Dim dtCountry As New DataTable("Countries")
Dim dcIdCountry As New DataColumn("IDCountry")
Dim dcCountry As New DataColumn("Country")
dtCountry.Columns.Add(dcIdCountry)
dtCountry.Columns.Add(dcCountry)
ds.Tables.Add(dtCountry)
For i As Integer = 1 To 5
Dim dr As DataRow = dtCountry.NewRow
dr(0) = i.ToString
dtCountry.Rows.Add(dr)
Next
dtCountry.Rows(0)(1) = "Austria(EU)"
dtCountry.Rows(1)(1) = "Germany(EU)"
dtCountry.Rows(2)(1) = "Holland(EU)"
dtCountry.Rows(3)(1) = "Georgia(US)"
dtCountry.Rows(4)(1) = "Other(US)"
End If
FillGrid()
End Sub
End Class
Public Class DataGridComboBoxColumn
Inherits DataGridTextBoxColumn
Public WithEvents ColumnComboBox As NoKeyUpCombo 'special class
Private WithEvents cmSource As CurrencyManager
Private mRowNum As Integer
Private isEditing As Boolean
Shared Sub New()
End Sub
Public Sub New()
MyBase.New()
ColumnComboBox = New NoKeyUpCombo
AddHandler ColumnComboBox.SelectionChangeCommitted, _
New EventHandler(AddressOf ComboStartEditing)
End Sub
Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager,
_
ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal readOnly1 As
Boolean, _
ByVal instantText As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, readOnly1, instantText,
cellIsVisible)
mRowNum = rowNum
cmSource = source
ColumnComboBox.Parent = Me.TextBox.Parent
ColumnComboBox.Location = Me.TextBox.Location
ColumnComboBox.Size = New Size(Me.TextBox.Size.Width,
ColumnComboBox.Size.Height)
ColumnComboBox.Text = Me.TextBox.Text
TextBox.Visible = False
ColumnComboBox.Visible = True
ColumnComboBox.BringToFront()
ColumnComboBox.Focus()
End Sub
Protected Overloads Overrides Function Commit(ByVal dataSource As _
CurrencyManager, ByVal rowNum As Integer) As Boolean
If isEditing Then
isEditing = False
SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text)
End If
Return True
End Function
Private Sub ComboStartEditing(ByVal sender As Object, ByVal e As
EventArgs)
isEditing = True
MyBase.ColumnStartedEditing(DirectCast(sender, Control))
End Sub
Private Sub LeaveComboBox(ByVal sender As Object, ByVal e As EventArgs)
_
Handles ColumnComboBox.Leave
If isEditing Then
SetColumnValueAtRow(cmSource, mRowNum, ColumnComboBox.Text)
isEditing = False
Invalidate()
End If
ColumnComboBox.Hide()
End Sub
End Class
Public Class NoKeyUpCombo
Inherits ComboBox
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg <> &H101 Then
MyBase.WndProc(m)
End If
End Sub
End Class
///
 
Back
Top