Set value to RadDropdownList

18 posts, 0 answers
  1. Lokesh
    Lokesh avatar
    89 posts
    Member since:
    Jul 2012

    Posted 09 Jul 2012 Link to this post

    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?

  2. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 11 Jul 2012 Link to this post

    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 >>
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Lokesh
    Lokesh avatar
    89 posts
    Member since:
    Jul 2012

    Posted 11 Jul 2012 Link to this post

    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..
  5. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 16 Jul 2012 Link to this post

    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 >>
  6. Juergen Holzer
    Juergen Holzer avatar
    32 posts
    Member since:
    Mar 2010

    Posted 27 Jul 2012 Link to this post

    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
  7. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 01 Aug 2012 Link to this post

    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 >>
  8. Juergen Holzer
    Juergen Holzer avatar
    32 posts
    Member since:
    Mar 2010

    Posted 01 Aug 2012 Link to this post

    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
  9. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 06 Aug 2012 Link to this post

    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 >>
  10. Juergen Holzer
    Juergen Holzer avatar
    32 posts
    Member since:
    Mar 2010

    Posted 17 Aug 2012 Link to this post

    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
  11. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 17 Aug 2012 Link to this post

    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 >>
  12. RJ
    RJ avatar
    386 posts
    Member since:
    Oct 2006

    Posted 06 Dec 2012 Link to this post

    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?
  13. Juergen Holzer
    Juergen Holzer avatar
    32 posts
    Member since:
    Mar 2010

    Posted 07 Dec 2012 Link to this post

    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.
  14. RJ
    RJ avatar
    386 posts
    Member since:
    Oct 2006

    Posted 07 Dec 2012 Link to this post

    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.

  15. Juergen Holzer
    Juergen Holzer avatar
    32 posts
    Member since:
    Mar 2010

    Posted 07 Dec 2012 Link to this post

    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.
  16. RJ
    RJ avatar
    386 posts
    Member since:
    Oct 2006

    Posted 08 Dec 2012 Link to this post

    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.  
  17. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 12 Dec 2012 Link to this post

    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.
  18. Timothy
    Timothy avatar
    37 posts
    Member since:
    Nov 2012

    Posted 26 Dec 2012 Link to this post

    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.
  19. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 27 Dec 2012 Link to this post

    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.
Back to Top
UI for WinForms is Visual Studio 2017 Ready