|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
ComboBox HELP Pleasse!item from the combobox (e.g., "Martin"), how can I access the value of that item (e.g., "1")? Dim dtAdjuster as new DataTable dtAdjuster.Columns.add("Names") dtAdjuster.Columns.add("Keys") dtAdjuster.loaddatarow(new object() {"Martin", "1"},true) dtAdjuster.loaddatarow(new object() {"Cor","2"},true) Combobox1.datasource = dt Combobox1.displaymember = "Names" Combobox1.Valuemember = "Keys" In the click method, I check the value of Combobox1.SelectedValue.ToString It is returning "System.Data.DataRowView" as the value of the item. What is wrong? Any idea? Thank you in advance. You are missing the key component of code that involves databinding. You
must call the DataBind() method after setting the DataSource property. In your case, it looks like you need the following line: Combobox1.DataBind() I think this will fix your problem, if not, let me know. Good Luck! Show quoteHide quote "D" <D@discussions.microsoft.com> wrote in message news:ECEA6448-3A70-4703-A7D5-AB286321B1D4@microsoft.com... >I am using the following code as an example. Once the user selects an > item from the combobox (e.g., "Martin"), how can I access the value of > that item (e.g., "1")? > > Dim dtAdjuster as new DataTable > dtAdjuster.Columns.add("Names") > dtAdjuster.Columns.add("Keys") > dtAdjuster.loaddatarow(new object() {"Martin", "1"},true) > dtAdjuster.loaddatarow(new object() {"Cor","2"},true) > > Combobox1.datasource = dt > Combobox1.displaymember = "Names" > Combobox1.Valuemember = "Keys" > > In the click method, I check the value of > > Combobox1.SelectedValue.ToString > > It is returning "System.Data.DataRowView" as the value of the > item. What is wrong? Any idea? > > Thank you in advance. > Nathan,
The DataBind is typical a webform method. A webform does not have a combobox luckily for that they have named that a little bit the same control DropDownList. I hope this helps, Cor D,
I copied and pasted your code into a VS 2003 Windows Form application. After fixing 1 typo, your code worked for me. I changed this line: Combobox1.datasource = dt to this line: ComboBox1.DataSource = dtAdjuster Kerry Moorman Show quoteHide quote "D" wrote: > I am using the following code as an example. Once the user selects an > item from the combobox (e.g., "Martin"), how can I access the value of > that item (e.g., "1")? > > Dim dtAdjuster as new DataTable > dtAdjuster.Columns.add("Names") > dtAdjuster.Columns.add("Keys") > dtAdjuster.loaddatarow(new object() {"Martin", "1"},true) > dtAdjuster.loaddatarow(new object() {"Cor","2"},true) > > Combobox1.datasource = dt > Combobox1.displaymember = "Names" > Combobox1.Valuemember = "Keys" > > In the click method, I check the value of > > Combobox1.SelectedValue.ToString > > It is returning "System.Data.DataRowView" as the value of the > item. What is wrong? Any idea? > > Thank you in advance. > Kerry,
It was some sample code I made, I did not see this one change. (I don't see the reason, this is the only change I use forever dt) Good catch, :-) CorShow quoteHide quote "Kerry Moorman" <KerryMoor***@discussions.microsoft.com> schreef in bericht news:B050E395-B0F9-4FB4-ADD7-4B11EE609DCD@microsoft.com... > D, > > I copied and pasted your code into a VS 2003 Windows Form application. > After > fixing 1 typo, your code worked for me. I changed this line: > > Combobox1.datasource = dt > > to this line: > > ComboBox1.DataSource = dtAdjuster > > Kerry Moorman > > > "D" wrote: > >> I am using the following code as an example. Once the user selects an >> item from the combobox (e.g., "Martin"), how can I access the value of >> that item (e.g., "1")? >> >> Dim dtAdjuster as new DataTable >> dtAdjuster.Columns.add("Names") >> dtAdjuster.Columns.add("Keys") >> dtAdjuster.loaddatarow(new object() {"Martin", "1"},true) >> dtAdjuster.loaddatarow(new object() {"Cor","2"},true) >> >> Combobox1.datasource = dt >> Combobox1.displaymember = "Names" >> Combobox1.Valuemember = "Keys" >> >> In the click method, I check the value of >> >> Combobox1.SelectedValue.ToString >> >> It is returning "System.Data.DataRowView" as the value of the >> item. What is wrong? Any idea? >> >> Thank you in advance. >> |
|||||||||||||||||||||||