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

RadGridView ComboBox source of data

1 Answer 150 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Szymon
Top achievements
Rank 1
Szymon asked on 17 Sep 2013, 09:48 AM
Hi!

I have a RadGridView with ComboBox column, Whole Grid is binded to DataTable.
In comboboxes I would like to have options like: "MS Access", "MS Excel", and a few more. These are options user can choose, and these options are not in DataTable from beginning. How can I populate binded ComboBox with different values than from DataTable? 

Thanks in advance!

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 20 Sep 2013, 07:33 AM
Hello Szymon,

Thank you for contacting Telerik Support.

In order to achieve your goal, it is necessary to set DataSource property of your GridViewComboBoxColumn:
List<string> options = new List<string>() { "MS Access", "MS Excel", "MS Word" };
 
public Form1()
{
    InitializeComponent();
 
    List<MyObject> myList = new List<MyObject>();
    myList.Add(new MyObject(1, "Outdoor","MS Access"));
    myList.Add(new MyObject(2, "Hardware","MS Excel"));
    myList.Add(new MyObject(3, "Tools","MS Access"));
    myList.Add(new MyObject(4, "Books","MS Excel"));
    myList.Add(new MyObject(5, "Appliances","MS Word"));
    radGridView1.DataSource = myList;
 
    this.radGridView1.Columns.RemoveAt(2);
    GridViewComboBoxColumn comboColumn = new GridViewComboBoxColumn("MyOption");
    comboColumn.DataSource = options;
    this.radGridView1.Columns.Add(comboColumn);
}

It is not exactly obligatory to have a GridViewComboBoxColumn in order to have several options to choose in edit mode. I have prepared a sample example below to demonstrate how to change the default editor even of a GridViewTextBoxColumn and replace it with a RadDropDownListEditor which DataSource can be populated with data as you wish:
List<string> options = new List<string>() { "MS Access", "MS Excel", "MS Word" };
 
public Form1()
{
    InitializeComponent();
 
    List<MyObject> myList = new List<MyObject>();
    myList.Add(new MyObject(1, "Outdoor","MS Access"));
    myList.Add(new MyObject(2, "Hardware","MS Excel"));
    myList.Add(new MyObject(3, "Tools","MS Access"));
    myList.Add(new MyObject(4, "Books","MS Excel"));
    myList.Add(new MyObject(5, "Appliances","MS Word"));
    radGridView1.DataSource = myList;
 
    radGridView1.EditorRequired += radGridView1_EditorRequired;
}
 
private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
{
    if (radGridView1.CurrentColumn.Name == "MyOption")
    {
        RadDropDownListEditor dropdownEditor = new RadDropDownListEditor();
        ((RadDropDownListEditorElement)(dropdownEditor.EditorElement)).DataSource = options;
        dropdownEditor.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
        e.Editor = dropdownEditor;
    }
}
 
public class MyObject
{
    public MyObject(int myInt, string myString, string myOption)
    {
        _myInt = myInt;
        _myString = myString;
        _myOption = myOption;
    }
 
    private int _myInt;
 
    public int MyInt
    {
        get
        {
            return _myInt;
        }
        set
        {
            _myInt = value;
        }
    }
 
    private string _myString;
 
    public string MyString
    {
        get
        {
            return _myString;
        }
        set
        {
            _myString = value;
        }
    }
 
    private string _myOption;
 
    public string MyOption
    {
        get
        {
            return _myOption;
        }
        set
        {
            _myOption = value;
        }
    }
}

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
Szymon
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or