hi,
how can i add data to a datagridcombo box column from a mdf file,
i want to take the first name of all employees and display in the datagrid view combo box column:
is the code below correct , please can u tell me what should i do to suite to the above problem

thanx

db.openConnection();
            String query = @"Select FirstName from Emplooyee";
            SqlCommand command = new SqlCommand(query,DB.getConnection());

            SqlDataAdapter adapter = new SqlDataAdapter();
            DataTable dt = new DataTable();

            adapter.Fill(dt);
            dgvPAction.Rows[0].Cells[1].Value = adapter.Fill(dt).ToString();

Recommended Answers

Instead of dgvPAction.Rows[0].Cells[1].Value = adapter.Fill(dt).ToString(); ,

Use the following code:

DataGridViewComboBoxColumn comboBox = (DataGridViewComboBoxColumn)dgvPAction.Rows[0].Cells[1].OwningColumn;

comboBox.DataSource = dt;

Thanks

Jump to Post

Also add these:

Replace ColumnName with the column name

comboBox.DisplayMember = "[I]ColumnName[/I]";

Thanks

Jump to Post

Here's the full code:

db.openConnection();

  DataGridViewComboBoxColumn comboBox = (DataGridViewComboBoxColumn)dgvPAction.Rows[0].Cells[1].OwningColumn;
  String query = @"Select FirstName from Emplooyee";
  SqlCommand command = new SqlCommand(query,DB.getConnection());

  SqlDataReader dataReader = command.ExecuteReader();

  while (dataReader.Read())
      {
               comboBox.Items.Add(reader["colAssignTo"]);
      }

Thanks

Jump to Post

Oh yea. Your query only selects "FirstName" from the table. Is that what you want to add to the comboBox?

If so, then this will work:

db.openConnection();

  DataGridViewComboBoxColumn comboBox = (DataGridViewComboBoxColumn)dgvPAction.Rows[0].Cells[1].OwningColumn;
  String query = @"Select FirstName from Emplooyee";
  SqlCommand command = new SqlCommand(query,DB.getConnection());

  SqlDataReader dataReader = …
Jump to Post

All 13 Replies

Instead of dgvPAction.Rows[0].Cells[1].Value = adapter.Fill(dt).ToString(); ,

Use the following code:

DataGridViewComboBoxColumn comboBox = (DataGridViewComboBoxColumn)dgvPAction.Rows[0].Cells[1].OwningColumn;

comboBox.DataSource = dt;

Thanks

hi,
the values does not come instead there is a error message in the datagridview combo box with System.Data...... as such

why is this happening???

Also add these:

Replace ColumnName with the column name

comboBox.DisplayMember = "[I]ColumnName[/I]";

Thanks

hi ,

it says that the field does not exists.

i went to the small icon in the datagridview and then edit column and selected the column in the left corner and then and changed the Name under Design to colAssignTo.

so my code would be

comboBox.DisplayMember = "colAssignTo";

this gives the error which i mentioned above, what is the problem in this???
please help me.

thanxxxxxxx

you could also use a data reader to store the result.

datareader = cmd.executereader();
while(datareader.read)
{
cmb.items.add(datareader.getValue(0));
}

can u give me the full code please

Here's the full code:

db.openConnection();

  DataGridViewComboBoxColumn comboBox = (DataGridViewComboBoxColumn)dgvPAction.Rows[0].Cells[1].OwningColumn;
  String query = @"Select FirstName from Emplooyee";
  SqlCommand command = new SqlCommand(query,DB.getConnection());

  SqlDataReader dataReader = command.ExecuteReader();

  while (dataReader.Read())
      {
               comboBox.Items.Add(reader["colAssignTo"]);
      }

Thanks

hey,
line 11 shold be:
comboBox.Items.Add(dataReader["colAssignTo"]);

not like "comboBox.Items.Add(reader["colAssignTo"]); " this right?

there is an array out of bound exception occuring? why is that highlighting the colAssignTo??

please help me??

Oh yea. Your query only selects "FirstName" from the table. Is that what you want to add to the comboBox?

If so, then this will work:

db.openConnection();

  DataGridViewComboBoxColumn comboBox = (DataGridViewComboBoxColumn)dgvPAction.Rows[0].Cells[1].OwningColumn;
  String query = @"Select FirstName from Emplooyee";
  SqlCommand command = new SqlCommand(query,DB.getConnection());

  SqlDataReader dataReader = command.ExecuteReader();

  while (dataReader.Read())
      {
               comboBox.Items.Add(reader["FirstName"]);
      }

Thanks

commented: You are very kind to this OP +4

or inside the while loop u can also write

comboBox.Items.Add(datareader.getString(0));

what is dgvPAction ?

and DB whose object?

Can u give me a download link for that? Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.