I'm having a problem with filling a comboxcolumn in a datagrid view. Here is a brief description of my problem.
I've a combo column in datagrid view named as dgvRightsColumn and table in SQL named as Rights. I want to fill this combobox of dagaridview with the RightsNames in Rights Table.
DataGridViewComboBoxColumn dgvRightsColumn = new DataGridViewComboBoxColumn();
SqlCommand fillRights = new SqlCommand("SELECT * FROM [Rights]", sqlConnection);
SqlDataReader readerRights = fillRights.ExecuteReader();
while (readerRights.Read())
{
dgvRightsColumn.Items.Add(Convert.ToString(readerRights["RightName"]));
}
readerRights.Close();
Problem : you are just creating the object for
DataGridViewComboBoxColumnbut not specifying the actual column to be considered from Gridview as ComboBox that is 3rd column.Solution : you need to
castthe required column fromDataGridviewto theDataGridViewComboBoxColumnto insert the Items.Replace This :
With This:
Note : You are not opening the
SqlConnectoionObjectsqlConnectionbefore Executing theExecuteReadercommand.You need to open it as below :Complete Code:
Try This :