This is a migrated thread and some comments may be shown as answers.

Set value to RadDropdownList

17 Answers 3704 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Lokesh
Top achievements
Rank 1
Lokesh asked on 09 Jul 2012, 08:29 AM
Hi Team,
I am new to winform development.
I have bound the RadDropdownList with values from database with DisplayMember and ValueMember properties set properly.
I can see the items in dropdown with no problem. 
But when I want to assign the value to RadDropdownList, it sets always to 0 (zero).

Please take a look at my code to bind dropdown :
public void FillInvoiceTypeCombo(int EventTypeID)
 {
     object businessObject = null;
      if (EventTypeID == GlobalConstants.ACCOUNTJOURNAL_EVENT_TYPE_ID_FOR_AP_INVOICE)
          businessObject = VoucherDocumentTypeNameValue.GetVoucherTypeNameValue(GlobalConstants.ACCOUNTJOURNAL_EVENT_TYPE_ID_FOR_AP_INVOICE);
      else
            businessObject = VoucherDocumentTypeNameValue.GetVoucherTypeNameValue(GlobalConstants.ACCOUNTJOURNAL_EVENT_TYPE_ID_FOR_PO_WO_SERVICING);
 
     cmbInvoiceType.DataSource = (VoucherDocumentTypeNameValue)businessObject;
     cmbInvoiceType.DisplayMember = "Value";
     cmbInvoiceType.ValueMember = "Key";
}


And in another method, I am setting the value of dropdown :
pub void SetControls()
{
 // Fill DataSet ds with values from db
 
FillInvoiceTypeCombo(int.Parse(ds.Tables[0].Rows[0]["AccountJournalEventTypeID"].ToString()));
cmbInvoiceType.SelectedValue = int.Parse(ds.Tables[0].Rows[0]["InvoiceTypeID"].ToString());
}

But every time I get SelectedValue as 0 (zero).

Can you please tell me how can I set the value of the RadDropdownList?

17 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 11 Jul 2012, 11:57 AM
Hello Lokesh,

Thank you for writing.

The SelectedValue property will select an item with the specified value if such exists. Here is a sample where I have three items an upon button click I am selecting the item with value 3:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
 
        BusinessObject o1 = new BusinessObject() { ID = 1, Name = "Adam" };
        BusinessObject o2 = new BusinessObject() { ID = 2, Name = "Brandon" };
        BusinessObject o3 = new BusinessObject() { ID = 3, Name = "Charlie" };
 
        BindingList<BusinessObject> list = new BindingList<BusinessObject>();
        list.Add(o1);
        list.Add(o2);
        list.Add(o3);
 
        radDropDownList1.DataSource = list;
        radDropDownList1.DisplayMember = "Name";
        radDropDownList1.ValueMember = "ID";
    }
 
    private void radButton1_Click(object sender, EventArgs e)
    {
        radDropDownList1.SelectedValue = 3;
    }
}
 
class BusinessObject
{
    public int ID { get; set; }
    public string Name { get; set; }
}

I hope that you find this information useful. Let me know if I can be of further assistance.
 
Greetings,
Stefan
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Lokesh
Top achievements
Rank 1
answered on 11 Jul 2012, 12:02 PM
Hi Stefan,
Thanks for your reply.
But Its not working at my end.
I tried the same solution u gave but no luck. I get the selected value as 0 (zero) always.
Don't know whats going wrong. Same thing works for web application but not for winform.

Regards,
Lok..
0
Stefan
Telerik team
answered on 16 Jul 2012, 05:51 AM
Hi Lokesh,

In order to allow me to help you with your case I will need you to provide me with your application or a sample one, where the issue can be reproduced. For this reason, I would like to kindly ask you to open a new support ticket and attach the solution there. Please make sure that you reference this thread in your ticket.

I am looking forward to your sample project.
 
All the best,
Stefan
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Juergen Holzer
Top achievements
Rank 2
answered on 27 Jul 2012, 01:05 PM
Hello guys!

Is there a solution for this issue?

I have the same one and the only way I got it to work is with
radDropDownList1.SelectedIndex = radDropDownList1.DropDownListElement.FindStringExact("3");

Kind regards
Juergen
0
Stefan
Telerik team
answered on 01 Aug 2012, 08:57 AM
Hi Juergen,

I was not able to reproduce the behavior reported by Lokesh on my end, thus I did not confirm any issue. If you think that there might be one, please get back to us with a scenario that we can use to reproduce it, or open a new support ticket, where you can attach a sample project. 

Thank you in advance for your time and cooperation.

Greetings,
Stefan
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Juergen Holzer
Top achievements
Rank 2
answered on 01 Aug 2012, 10:54 AM
Hello!

I think I found out something interesting:

Setting the DisplayMember and ValueMember with the Designer has no effect.

I'm populating the DropDownList in my Window_Load Eventhandler with the DataSource property and a Generic List.
To Display the right Information and to get the SelectedValue, I also have to set ValueMember and DisplayMember right before setting the DataSource.

If this information is too less or I'm wrong, I'll do my best to reproduce it in a sample project. (I'm using 2012Q2 Release)

Kind Regards
Juergen
0
Stefan
Telerik team
answered on 06 Aug 2012, 07:19 AM
Hi Juergen,

I have tested the scenario mentioned, however I am able to select an item via its DisplayMember of ValueMember property. When using the SelectedIndex property together with the FindStringExact method you should specify the DisplayMember of an item, while with the SelectedValue, you should specify the value of it.

If you continue experiencing this behavior, please open a new support ticket and provide me with your project there, so I can look into it.
 
Greetings,
Stefan
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Juergen Holzer
Top achievements
Rank 2
answered on 17 Aug 2012, 01:37 PM
Hello again!

Sorry for my delay! I created a new sampleproject and there it's working for me too. So I reassigned and re-set my mainproject and now it's working there also, maybe there was something wrong with the designer or something else.

Thank you very much for your help!
Kind regards and have a nice weekend
Juergen
0
Stefan
Telerik team
answered on 17 Aug 2012, 02:54 PM
I am glad that everything is working fine on your end now. You have a good weekend too.

Greetings,
Stefan
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
RJ
Top achievements
Rank 1
answered on 06 Dec 2012, 10:13 PM
Im have same problem..
Selectedvalue does not work. Q2 2012

Only the first 4 of my raddropdownlist works with selectedvalue and the rest of 20 raddropdownlist stays at selectedindex -1

so the solution is to recreate the disgner page ro just create a new winform/telerik form?
0
Juergen Holzer
Top achievements
Rank 2
answered on 07 Dec 2012, 09:34 AM
Hello!

I think the beste solution is to remove the notworking dropdownlists, save project, close and reopen visualstudio und add the dropdownlists again. In my scenario I didn't have to recreate the designer page.
0
RJ
Top achievements
Rank 1
answered on 07 Dec 2012, 03:05 PM
Got the solution.
Its a RadDropdownList bug (Q2 2012) when you use datasource when you populate your RadDDL

But if you populate your RadDDL with  RadLIstDataItem, theres no bug on setting your RadDDL to a value.

0
Juergen Holzer
Top achievements
Rank 2
answered on 07 Dec 2012, 04:12 PM
This was my first thought too, but then I readded de RadDDL and setting the value was working with the DataSource too.
Did you try it?

You also have to set ValueMember and DisplayMember with the designer.
0
RJ
Top achievements
Rank 1
answered on 08 Dec 2012, 09:17 AM
My initial setup is all using datasource but then it didnt work for all my RadDDL and it took me days on wondering why its not working. I also did change it to wincontrols combobox and there were no problem but I insisted on using the RadDDL coz  of its looks and more features. I did several tweaks and I ended up using RadListDataItem which work for all of my RadDDL. So I converted all to use RadListDataItem.  
0
Peter
Telerik team
answered on 12 Dec 2012, 03:13 PM
Hello,

You should set the SelectedValue property to a value of exactly the same type as the ValueMember column. For example, if the ValueMember column is of type Short and you set the SelectedValue to 15 which is Integer, RadListDataSource cannot find matches between the types and the SelectedValue fails.

'not correct
radDropDownList.SelectedValue = 15
 
'correct
radDropDownList.SelectedValue = short(15)

I hope this helps Regards,
Peter
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
0
Timothy
Top achievements
Rank 1
answered on 26 Dec 2012, 11:49 PM
It seems I read in one of the forums here that you try to mimic Microsoft's implementation of similar properties when possible.

Is it possible you can fix this so that it makes the conversion automatically if the conversion is possible? That is, I understand you can't convert 50000 to a short but you could 15000.

I use VB.NET and I've never had to worry about this with their combo box (unless Option Explicit is turned on).

Thanks.
0
Peter
Telerik team
answered on 27 Dec 2012, 03:02 PM
Hi Timothy,

Thank you for writing.

We do not make such conversation automatically because there are a numerous cases of possible conversations and scenarios (only for example the value can be a business object that can be cast both to string and to integer).

Furthermore this conversation will significantly slow down the performance of the control. So we have decided to use a object from the user without attempting for automatically convert.

I hope that you find this information useful.

Kind regards,
Peter
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
Tags
DropDownList
Asked by
Lokesh
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Lokesh
Top achievements
Rank 1
Juergen Holzer
Top achievements
Rank 2
RJ
Top achievements
Rank 1
Peter
Telerik team
Timothy
Top achievements
Rank 1
Share this question
or