newbie: How to retreive string from ComboBox.SelectedItem?

newbie: How to retreive string from ComboBox.SelectedItem?

Post by deko » Fri, 15 Jul 2005 07:19:02


ComboBox cbo = this.cboParameter;
strParameterValue = cbo.SelectedItem.ToString; //does not work

How do I retrieve the string value of the selected item in my ComboBox?

Thanks in advance.
 
 
 

newbie: How to retreive string from ComboBox.SelectedItem?

Post by Herfried K » Fri, 15 Jul 2005 07:24:31

"deko" < XXXX@XXXXX.COM > schrieb:

If you added string objects to the 'Items' collection:

\\\
ParameterValue = (string)cbo.SelectedItem;
///

--
M S Herfried K. Wagner
M V P <URL: http://www.yqcomputer.com/ ;
V B <URL: http://www.yqcomputer.com/ ;

 
 
 

newbie: How to retreive string from ComboBox.SelectedItem?

Post by deko » Fri, 15 Jul 2005 07:29:23

Thanks for the quick reply.


The DataSource of the ComboBox is filled from a DataSet Table, retreived
from an Access database


I get "InvalidCastException"
 
 
 

newbie: How to retreive string from ComboBox.SelectedItem?

Post by Tim Wilso » Fri, 15 Jul 2005 07:35:35

I assume that the ComboBox is data bound. You can use the "Text" property of
the ComboBox or you can do something like this...

DataRowView drv = this.comboBox1.SelectedItem as DataRowView;
if (drv != null)
{
MessageBox.Show(drv.Row[this.comboBox1.DisplayMember].ToString());
}

--
Tim Wilson
.Net Compact Framework MVP
 
 
 

newbie: How to retreive string from ComboBox.SelectedItem?

Post by Herfried K » Fri, 15 Jul 2005 07:38:31

"deko" < XXXX@XXXXX.COM > schrieb:

How do you fill your combobox?

--
M S Herfried K. Wagner
M V P <URL: http://www.yqcomputer.com/ ;
V B <URL: http://www.yqcomputer.com/ ;
 
 
 

newbie: How to retreive string from ComboBox.SelectedItem?

Post by deko » Fri, 15 Jul 2005 07:45:19

>I assume that the ComboBox is data bound. You can use the "Text" property

Yes, the cbo is filled from a dataset table that is retreived from an access
database


I get "ArgumentException"

Column '' does not belong to table cboData

Seems like this should be simple... not in Kansas (VB) anymore!
 
 
 

newbie: How to retreive string from ComboBox.SelectedItem?

Post by deko » Fri, 15 Jul 2005 07:47:47

>>> \\\

cmdOle = new OleDbCommand("SelectDistinctProductColors_sp", cnxOle);
cmdOle.CommandType = CommandType.StoredProcedure;
daOle = new OleDbDataAdapter(cmdOle);
ds = new DataSet();
daOle.Fill(ds, "cboData");
cbo = this.cboProductColor;
cbo.DataSource = ds.Tables[0];
cbo.DisplayMember = "ProductColor";
cbo.ValueMember = "ProductColor";
 
 
 

newbie: How to retreive string from ComboBox.SelectedItem?

Post by Tim Wilso » Fri, 15 Jul 2005 08:01:23

Did you try the "Text" property first? Have you set the "DisplayMember"
property to the column to display?

--
Tim Wilson
.Net Compact Framework MVP



access
 
 
 

newbie: How to retreive string from ComboBox.SelectedItem?

Post by deko » Fri, 15 Jul 2005 08:09:12

> Did you try the "Text" property first?

yes, and many other things


no - at least not on purpose...

how do I set the DisplayMember property to the column to display?
 
 
 

newbie: How to retreive string from ComboBox.SelectedItem?

Post by Tim Wilso » Fri, 15 Jul 2005 08:25:10

From your other post it looks like you're already setting it. So, for
example, the code below creates a DataTable, adds a column, adds two rows,
and then sets the data source and the name of the column to display in the
control...

DataTable dataTable1 = new DataTable("MyTable");
dataTable1.Columns.Add("MyColumn", typeof(string));
dataTable1.Rows.Add(new object[] {"Item 1"});
dataTable1.Rows.Add(new object[] {"Item 2"});

this.comboBox1.DataSource = dataTable1;
this.comboBox1.DisplayMember = "MyColumn";

... and, after an item has been selected, the code below would display the
visible text...

MessageBox.Show(this.comboBox1.Text);

--
Tim Wilson
.Net Compact Framework MVP
 
 
 

newbie: How to retreive string from ComboBox.SelectedItem?

Post by deko » Fri, 15 Jul 2005 08:54:23

> From your other post it looks like you're already setting it. So, for

So it sounds like the DisplayMember property of the cbo (string) should be
the same as the name of the data table to which the cbo is bound to - is
this correct?

And , cbo.Text should return a string value that represents the selected
item as long as the above is true?

And does this mean that DispalyMember name (in your example, "MyColumn")
acts as the header row - that is, when nothing is selected in the cbo,
"MyColumn" will be displayed?

Thanks for the help!
 
 
 

newbie: How to retreive string from ComboBox.SelectedItem?

Post by Tim Wilso » Fri, 15 Jul 2005 09:08:35

> So it sounds like the DisplayMember property of the cbo (string) should be
No. It should be the name of the column that should be displayed in the
control.

The "Text" property returns the text that is shown in the "edit" portion of
the control - bound or unbound.

No. By default, the value in the "DisplayMember" column of the first row of
the data table will be selected, or nothing if there are no rows.

--
Tim Wilson
.Net Compact Framework MVP



rows,
the
the
 
 
 

newbie: How to retreive string from ComboBox.SelectedItem?

Post by deko » Fri, 15 Jul 2005 09:30:57

>> So it sounds like the DisplayMember property of the cbo (string) should

I think I was mixing up table and column. So - the dataset, datatable, and
table column should all have different names, and the name of the data
column is what shd be used for the DisplayMember property of the cbo?


Funny how that did not work for me the first time. will investigate.


So, after creating the dataset and binding a datatable to the DataSource of
the cbo, the first item in the datatable will appear in the cbo? How would
I get something like "Select a Product Color" to appear - that is, only when
the DataSource is first set. After the user selects something, I would
expect the cbo to reflect the last selection.

Thanks again for the help.
 
 
 

newbie: How to retreive string from ComboBox.SelectedItem?

Post by Tim Wilso » Fri, 15 Jul 2005 10:10:23

> the name of the data column is what shd be used
Yes.

Yes.

If the "DropDownStyle" property is set to "DropDown", which is the default,
then you can assign your string to the "Text" property in an event handler
for the Form Load event...

private void Form1_Load(object sender, System.EventArgs e)
{
this.comboBox1.Text = "Select a Product Color";
}

--
Tim Wilson
.Net Compact Framework MVP



is
and
selected
"MyColumn")
of
would
when