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

Combobox in a DataForm

1 Answer 55 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
Edward
Top achievements
Rank 1
Edward asked on 15 Jul 2015, 08:09 PM

Probably a very noob question: 

Supposed I have a RadComboBox in the ItemTemplate of a RadDataForm.

The entity "Client" have a column called "genderID", which should be bound to the dataform.

Another entity "DefGender" should be bound to the combobox, with "genderID" as the DataValueField, "genderText" as the DataTextField, and I want to somehow bind the SelectedValue to Client.genderID

How would you do this pragmatically?  It was pretty straight forward in Silverlight, but I couldn't figure this out in ASP.

 Thanks.

1 Answer, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 20 Jul 2015, 10:31 AM
Hi Edward,

You can use the following approach to achieve this requirement:
<telerik:RadComboBox ID="RadComboBox1" runat="server" OnDataBinding="RadComboBox1_DataBinding"></telerik:RadComboBox>
C#:
protected void RadComboBox1_DataBinding(object sender, EventArgs e)
{
    RadComboBox combo = (RadComboBox)sender;
    RadDataFormDataItem item = (RadDataFormDataItem)combo.NamingContainer;
 
    // acceess unique item ID
    string customerID = item.GetDataKeyValue("CustomerID").ToString();
 
    // apply combo settings
    combo.DataTextField = "TextField";
    combo.DataValueField = "ValueField";
 
    // obtain and set the data source depending on the unique ID
    var dummySource = Enumerable.Range(1, 6).Select(
        x => new { ValueField = x, TextField = "C: " + customerID + ", Book: " + x });
 
    combo.DataSource = dummySource;
 
    // select item if present
    combo.DataBound += (s, a) =>
    {
        RadComboBoxItem comboItem = combo.FindItemByValue("3");
        if (comboItem != null)
        {
            comboItem.Selected = true;
        }
    };
}

Hope this helps. Please give it a try and let me know about the result.

Regards,
Eyup
Telerik
Tags
DataForm
Asked by
Edward
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or