Converting Column in DataGridView into a ComboBox

点点圈 提交于 2021-02-08 05:15:59

问题


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'.


回答1:


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.




回答2:


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.




回答3:


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

MyTacTics.Blogspot.comenter image description here



来源:https://stackoverflow.com/questions/21333320/converting-column-in-datagridview-into-a-combobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!