2

I have a WinForms application written in C# in which I have a DataGridView bound to a DataSource populated from a SQL Database.

My code is as follows -

string sqlText = "SELECT columns FROM table;";
SqlCommand sqlCom = new SqlCommand(sqlText);
DataTable table = new DataTable();
SqlConnection linkToDB = DatabaseConnection();
sqlCom.Connection = linkToDB;
linkToDB.Open();
using (linkToDB)
using (SqlDataAdapter adapter = new SqlDataAdapter(sqlCom))
{
    adapter.Fill(table);
}     
dataMyGridView.DataSource = table;

I have tried to add a DataGridViewComboBox to this as follows -

DataGridViewComboBoxColumn colBox = new DataGridViewComboBoxColumn();
colBox.DataSource = GetListOfValues();   
dataMyGridView.Columns.Add(colbox);

Now this works, but it adds an entirely new column with the ComboBox, rather than converting one of the existing columns into a ComboBox.

Can anyone explain how I convert an existing Column in the dataMyGridView into a ComboBox?

The GetListOfValues function simply returns a DataTable containing all the possible values for the ComboBox field. It populates correctly, but like I said, its an entirely new column. How do I refer the added ComboBox to the relevant property in 'table'.

PJW
  • 5,197
  • 19
  • 60
  • 74
  • Not sure if this would actually work but you could try add two column (TextBox and ComboBox) in design mode and when you execute your code delete the one you don't need once you have bound the data. – Mo Patel Jan 24 '14 at 13:35
  • hi what exactly u want.. like in case of gender u want a dropdown with Male and Female option and when u display from table to grid, in grid u want combobox not text value.. right ?? – Deepak Sharma Jan 24 '14 at 15:19

3 Answers3

2

I answered a similar question today

Once a column is created you can't change its type. You can achieve your goal in two ways.

1.Create the required column, add it to the grid and bind the data. If you set the DataPropertyName property of your column then when your data is bound that data column will get bound to your grid column. The DataGridView will generate other columns automatically. Bind a second datasource to the comboboxcolumn itself.

2.Modify your database such that the list is provided by the database and binding will automatically create comboboxcolumn.

Community
  • 1
  • 1
Junaith
  • 3,298
  • 24
  • 34
  • How to bind to 2 datasources? I want to have a combo column in the grid that is bound to datasource1(the grid is bound to datasource1. The column is bound to one property of datasource 1). Take all the possible values for the combos in the column from a datasource2. When a selection is made, in any combo in that column, the result to propagade to datasource1 property. – Mediarea Jan 30 '19 at 11:06
1

Yes, it is true that once your DataGridView is populated, one cannot change the column type. But you achieve your goal by doing following steps. 1.

                dataView.Columns["yourColoumn"].Visible = false;
                DataGridViewComboBoxColumn cmbCol = new DataGridViewComboBoxColumn();
                cmbCol.HeaderText = "yourColumn";
                cmbCol.Name = "myComboColumn";
                cmbCol.Items.Add("True");
                cmbCol.DataSource = myList();
                dataView.Columns.Add(cmbCol);
                dataView.Columns["myComboColumn"].DisplayIndex = "at any index that you want";

                foreach (DataGridViewRow row in dataView.Rows)
                {
                    row.Cells["myComboColumn"].Value = row.Cells["yourColumn"].Value;
                } 

. 2. Now you have to do only one thing, raise one more event.

    private void dataView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                try
                {
                    dataView.Rows[e.RowIndex].Cells["yourColoumn"].Value = dataView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                }
                catch (Exception ex)
                {
                }
             }

Actually you i have simply created a copy of "yourColoumn" in a combobox column. and in the backend "yourColoumn" is being update.

Pranay Deep
  • 1,371
  • 4
  • 24
  • 44
-1

Hi I have such post please check it out.. its perfect with sample code.

MyTacTics.Blogspot.comenter image description here

Deepak Sharma
  • 4,124
  • 1
  • 14
  • 31
  • Thanks for your post, but I getting an error using your method. Please take a look at http://stackoverflow.com/questions/36646686/set-datagridviewcombobox-default-equal-to-existing-datagridview-column – Zeus Apr 15 '16 at 21:50