-1

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();
rae1
  • 6,066
  • 4
  • 27
  • 48
Azeem Khalid
  • 145
  • 1
  • 1
  • 7
  • What is the issue you are having? – rae1 Dec 10 '13 at 15:44
  • is it winform? or Webform? – Sudhakar Tillapudi Dec 10 '13 at 15:45
  • thanks for replying @rae1n. This code is not adding items in combo box. When I run this code. Combobox is still blank – Azeem Khalid Dec 10 '13 at 15:45
  • @SudhakarTillapudi it is a winform in C# – Azeem Khalid Dec 10 '13 at 15:46
  • 1
    It looks as if you are using a DataGridViewComboBoxColumn without a DataGridView. Your combo instance is created and then disappears in the GC. BTW: are you looking for a ComboBox? Take a look at data binding mechanisms. They make life much easier. – TomB Dec 10 '13 at 15:51
  • @TomB, I do have a DataGridView named as dgvTasksRights and in this GridView I've Four column, 1st Task Id, 2nd Task Name, 3rd Rights (Combobox Column), 4th CheckboxColumn. I'm just trying to assign a task to a specific user with specific permissions (Rights, that will be selected from combobox and Tasks will be assigned by checkbox.checked). I want to assign multiple tasks to one user, for this reason I've made gridview that contains the list of tasks. It also contains a combobx in each row to assign different rights on different tasks and a checkbox to select a particular task. – Azeem Khalid Dec 10 '13 at 15:55
  • @AzeemKhalid: you need to cast the required column from `DataGridview` to the `DataGridViewComboBoxColumn` , check my answer below. – Sudhakar Tillapudi Dec 10 '13 at 16:23

1 Answers1

2

Problem : you are just creating the object for DataGridViewComboBoxColumn but not specifying the actual column to be considered from Gridview as ComboBox that is 3rd column.

Solution : you need to cast the required column from DataGridview to the DataGridViewComboBoxColumn to insert the Items.

Replace This :

DataGridViewComboBoxColumn dgvRightsColumn = new DataGridViewComboBoxColumn();

With This:

DataGridViewComboBoxColumn dgvRightsColumn= dgvTasksRights.Columns[2] as DataGridViewComboBoxColumn;

Note : You are not opening the SqlConnectoion Object sqlConnection before Executing the ExecuteReader command.You need to open it as below :

sqlConnection.Open();

Complete Code:
Try This :

SqlCommand fillRights = new SqlCommand("SELECT * FROM [Rights]", sqlConnection);
sqlConnection.Open();
SqlDataReader readerRights = fillRights.ExecuteReader();

DataGridViewComboBoxColumn dgvRightsColumn= dgvTasksRights.Columns[2] as DataGridViewComboBoxColumn;
while (readerRights.Read())
{                
 dgvRightsColumn.Items.Add(Convert.ToString(readerRights["RightName"]));
}  
readerRights.Close();
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67