Search code examples
c#datasourcedatagridviewcolumn

c# datagridview having different datasources for different columns


I have a datagridview which has multiple text columns and a single comboboxcolumn. I need to specify a datasource for the iems of the comboboxcolumn and another datasource for the other text columns. I have tried the following sample code but it does not work.

dgv1.DataSource = DataSet1.Tables[0];
string[] managerList = Array.ConvertAll(DataSet2.Select(), row => (string)row[0]);
comboboxcolumn1.DataSource = managerList;

The managerList array has the entries that are to be populated into the combobox but they never show up.

Is that so that I can not have a separate datasource for a comboboxcolumn and its parent datagridview?

Thanks in advance.


Solution

  • I have checked with the following code and it works fine

    string[] arr = "This Should Get Displayed".Split(); //managerList in your case           
    dataGridView1.DataSource = MyData(false);
    (dataGridView1.Columns["Column1"] as DataGridViewComboBoxColumn).DataSource = new BindingSource(arr, null); // Bind to the column of grid
    

    Can you try assigning it as referencing it as a column of your datagridview.

    This displays the text split on space in my combobox.

    When you are using string array no need to specify Display and Value member for the column

    EDIT

    If you have a DataTable you can also do it in this fashion

    (dataGridView1.Columns["Column1"] as DataGridViewComboBoxColumn).DataSource= datatable;
    (dataGridView1.Columns["Column1"] as DataGridViewComboBoxColumn).DisplayMember="columnname";
    (dataGridView1.Columns["Column1"] as DataGridViewComboBoxColumn).ValueMember="columnname";