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

Populate GridViewComboBox values from separate database table

1 Answer 51 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Justin
Top achievements
Rank 1
Justin asked on 15 Oct 2013, 04:09 AM
Two tables in my database are: Regions and Carriers.
In the Carriers table, there is a field called "Regions".

That 'regions' field will be a GridViewComboBox that should get it's values from a "name" field in the Carriers table.

How would I do this?

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 17 Oct 2013, 04:05 PM
Hello Justin,

Thank you for contacting Telerik Support.

In order to achieve your goal, please have a look at the following code snippet:
private void Form1_Load(object sender, EventArgs e)
{
    this.regionTableAdapter.Fill(this.deliveryDBDataSet1.Region);
    this.carrierTableAdapter.Fill(this.deliveryDBDataSet.Carrier);
 
    this.radGridView1.DataSource = this.deliveryDBDataSet.Carrier;
 
    this.radGridView1.EditorRequired += radGridView1_EditorRequired;
    this.radGridView1.CellEditorInitialized += radGridView1_CellEditorInitialized;
}
 
private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
    if (e.Column.Name == "Region")
    {
        RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
        RadDropDownListEditorElement element = (RadDropDownListEditorElement)editor.EditorElement;
        element.SelectedItem = GetInitialValue(e.Row.Cells["Region"].Value.ToString(), element.ListElement.Items);
    }
}
 
private RadListDataItem GetInitialValue(string cellText, IList<RadListDataItem> items)
{
    foreach (RadListDataItem item in items)
    {
        if (cellText == item.DisplayValue.ToString())
        {
            return item;
        }
    }
 
    return null;
}
 
private void radGridView1_EditorRequired(object sender, Telerik.WinControls.UI.EditorRequiredEventArgs e)
{
    GridViewEditManager manager = sender as GridViewEditManager;
 
    if (manager.GridViewElement.CurrentColumn.Name == "Region")
    {
        RadDropDownListEditor editor = new RadDropDownListEditor();
        RadDropDownListEditorElement element = editor.EditorElement as RadDropDownListEditorElement;
        element.DataSource = this.deliveryDBDataSet1.Region;
        element.DisplayMember = "Name";
        editor.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
        e.Editor = editor;
    }
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Justin
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or