Set default value of telerik:GridDropDownColumn inside of telerik:RadGrid

RSolberg picture RSolberg · Dec 14, 2009 · Viewed 7.1k times · Source

I have a telerik:RadGrid that is bound to a SQL Data Source. One of the columns is for "Location" which is really a look up value in another table.

<telerik:GridDropDownColumn 
     DataField="d_location_id" 
     DataSourceID="dsLocation" 
     UniqueName="d_location_id" 
     DataType="System.Int32" 
     ListValueField="d_location_id" 
     ListTextField="Abbreviation" 
     HeaderText="Location">
</telerik:GridDropDownColumn>

My list of locations is stored in an ObjectDataSource, which is bound to a static DataTable and sorted alphabetically for me already. What I would like to do is be able to set the default option for this dropdown.

For example, suppose I have the following locations:

1   Home    
2   Work
3   Parents
4   Car

I would like to have Parents be my default value.

This sample on Telerik shows something similar to what I'm trying to do. If you click "Add New Record", you'll notice the default city is Kirkland and I'm trying to figure out how to use London as the default when adding a new record.

Answer

Jon Erickson picture Jon Erickson · Dec 15, 2009

Not sure if it is the best or most straightforward way or not, but it does work.

protected void gridMyInfo_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item.IsInEditMode && e.Item.ItemIndex < 0)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        GridEditManager editMan = editedItem.EditManager;

        GridDropDownListColumnEditor editor = editMan.GetColumnEditor("d_location_id") as GridDropDownListColumnEditor;
        editor.ComboBoxControl.SelectedIndex = editor.ComboBoxControl.Items.FindItemIndexByText("Parents");
    }
}