Populate GridViewComboBox values from separate database table

2 posts, 0 answers
  1. Justin
    Justin avatar
    15 posts
    Member since:
    Jan 2012

    Posted 14 Oct 2013 Link to this post

    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?
  2. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 17 Oct 2013 Link to this post

    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 >>
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top