Assigning DateTime array to datasource

5 posts, 0 answers
  1. Espen
    Espen avatar
    11 posts
    Member since:
    Jan 2008

    Posted 23 Jan 2008 Link to this post

    Hi,

    I am having a small problem with the RadComboBox. I am assigning a DateTime array as datasource to a combobox, and when I get a SelectedIndexChanged event I'd like to get the selected object out again (pretty much so I can use the RadComboBox as a drop-in replacement for standard .net combobox). However, I am unable to get the object, as the SelectedValue is null, and since I use an array I can't set the ValueMember. Is there a way to accomplish this?

    The workaround I am using at the moment is this:

            private void drdDate_SelectedIndexChanged(object sender, EventArgs e)  
            {  
                if (drdDate.SelectedIndex >= 0)  
                {  
                    DateTime tstTime;  
                    if (DateTime.TryParse(((RadComboBoxItem)drdDate.SelectedItem).Text, out tstTime))  
                    {  
                        // do the processing here   
                    }  
                }  
            }          
     

    But I am really hoping I don't have to do text parsing for all the different comboboxes in the application.
  2. Espen
    Espen avatar
    11 posts
    Member since:
    Jan 2008

    Posted 24 Jan 2008 Link to this post

    Bah, forget I said anything.

    I changed the
    drdDate.Datasource = dateArray;

    to

    drdDate.BeginUpdate();  
    RadComboBoxItem cboItem;  
    foreach (DateTime dt in newList)  
    {  
        cboItem = new RadComboBoxItem();  
        cboItem.Text = dt.ToShortDateString();  
        cboItem.Value = dt;  
        drdDate.Items.Add(cboItem);  
    }  
    drdDate.EndUpdate();  
     

    and then I can get the object as before  in the index change event.

    object selObj = ((RadComboBoxItem)drdDate.SelectedItem).Value;  
    if (selObj is DateTime)  
    {  
        DateTime selTime = (DateTime)selObj;  
    }  
    else 
    {  
    ...  
    }  
     

    That should teach me to read docs before asking.... :)
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Georgi
    Admin
    Georgi avatar
    212 posts

    Posted 25 Jan 2008 Link to this post

    Hello Espen,

    Thank you for writing us.

    In fact, your initial solution is absolutely correct and ValueMember property can be set. All you have to do is to add code such as the one below when assigning the DateTime array as data source:

        radComboBox1.DisplayMember = "Date";
        radComboBox1.ValueMember = "Date";


    The SelectedValue property will then correctly return the selected value - in this case, it will be the value of the Date property of the currently selected object.

    If you have any other questions, please do not hesitate to write us again.

    Greetings,
    Georgi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  5. Espen
    Espen avatar
    11 posts
    Member since:
    Jan 2008

    Posted 25 Jan 2008 Link to this post

    Ah, excellent.

    However, this leads me to ask another question.

    If I have my own class and I use it as an array as before, how can I get the object returned?

    Say the class Weather have the following properties : Name, Latitude, Longitude, CloudCover

    If I set DisplayMemeber to "Name" it should use that property for the text. But if I also set ValueMember to "Name", will the SelectedValue be the object or just the string returned from Name ? 
  6. Georgi
    Admin
    Georgi avatar
    212 posts

    Posted 28 Jan 2008 Link to this post

    Hello Espen,

    If ValueMember property is set to the name of an item's member property, then SelectedValue for the currently selected item would always return the value of this property member.
    So, in your case if you set ValueMember to "Name" property, the SelectedValue will return the string returned from "Name" of the current item (i.e. the name of the current item).
    Usually, SelectedValue is set to return value which could be used as an identification key for an item in Items collection.

    Don't hesitate to contact us if you have further questions.

    Sincerely yours,
    Georgi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
Back to Top
UI for WinForms is Visual Studio 2017 Ready