Combobox.SELECTEDVALUE within a DataGrid

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a databound combobox within a datagrid. I have set the DisplayMember
and ValueMember.

BUT, how do I capture the Value (SelectedValue) when the user changes the
selection?
 
Sorry, I am using Windows forms.

The combobox contains the names of customers, with the DISPLAYMEMBER set to
CUSTOMERNAME and the VALUEMEMBER set to CUSTOMERKEY.

I haved tried creating a similar combobox outside the datagrid and then
seaching for the text values, but unfortunately the list I have to use has
multiple entries for the same client or clients with the same name, so a text
comparison doesn't work. I need to get at the SELECTEDVALUE of the combobox
so that I can get the customer key.
 
Are you having trouble getting a reference to the combo box in the grid or
having a problem discovering which item was selected having succesfully
obtaining a reference to the specific combo box in the grid?

pm
 
I reference the combobox in the datagrid by using the code:

datagrid1(datagrid1.currentcell.rownumber,
datagrid1.currentcell.columnnumber)

It's the second part I can't do - discovering which item was selected. I
want to obtain the SELECTEDVALUE of the combobox (ie.e the CUSTOMERKEY in the
example I've shown below).
 
private void showSelectedButton_Click(object sender, System.EventArgs e) {
int selectedIndex = comboBox1.SelectedIndex;
Object selectedItem = comboBox1.SelectedItem;

MessageBox.Show("Selected Item Text: " + selectedItem.ToString()
+ "\n" +
"Index: " + selectedIndex.ToString());
}
 
private void showSelectedButton_Click(object sender, System.EventArgs e) {
int selectedIndex = comboBox1.SelectedIndex;
Object selectedItem = comboBox1.SelectedItem;

MessageBox.Show("Selected Item Text: " + selectedItem.ToString()
+ "\n" +
"Index: " + selectedIndex.ToString());
}
 
Sorry, I'm still pretty green with .NET.

Two questions:

1. What does this code look like in VB.NET. I tried typing this in (removing
the {}), but I get an error on the Private Void line saying "End of Statement
expected"

2. More importantly, how does this work? I've never encountered "void"

Sorry to be a pain, your help is REALLY appreciated.
 
Private Sub showSelectedButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim selectedIndex As Integer
selectedIndex = comboBox1.SelectedIndex
Dim selectedItem As Object
selectedItem = comboBox1.SelectedItem

MessageBox.Show("Selected Item Text: " & selectedItem.ToString() &
Microsoft.VisualBasic.Constants.vbCrLf & _
"Index: " & selectedIndex.ToString())
 
Back
Top