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

Assigning DateTime array to datasource

4 Answers 84 Views
ComboBox and ListBox (obsolete as of Q2 2010)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Espen
Top achievements
Rank 1
Espen asked on 23 Jan 2008, 04:32 PM
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.

4 Answers, 1 is accepted

Sort by
0
Espen
Top achievements
Rank 1
answered on 24 Jan 2008, 10:47 AM
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.... :)
0
Georgi
Telerik team
answered on 25 Jan 2008, 05:06 PM
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
0
Espen
Top achievements
Rank 1
answered on 25 Jan 2008, 05:46 PM
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 ? 
0
Georgi
Telerik team
answered on 28 Jan 2008, 02:07 PM
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
Tags
ComboBox and ListBox (obsolete as of Q2 2010)
Asked by
Espen
Top achievements
Rank 1
Answers by
Espen
Top achievements
Rank 1
Georgi
Telerik team
Share this question
or