Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8283711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:50:13+00:00 2026-06-08T10:50:13+00:00

I have DataGridViewComboBoxCell and a DataTable. The data in Table I bound with DataGridViewComboBoxCell

  • 0

I have DataGridViewComboBoxCell and a DataTable. The data in Table I bound with DataGridViewComboBoxCell using DataSource and set ValueMember, and DisplayMember.

private void Form1_Load(object sender, EventArgs e)
{         
    DataGridViewComboBoxCell comboBoxCell = new DataGridViewComboBoxCell();

    dataGridView1.Rows[0].Cells[0] = comboBoxCell;

    comboBoxCell.DataSource = dataTable;
    comboBoxCell.ValueMember = "ID";
    comboBoxCell.DisplayMember = "Item";
}

How can I programmatically set the value in the cell when the form loads?
In the simple ComboBox I know a property SelectedIndex.
I tried comboBoxCell.Value = …; but it gives an exception.
And tried

private void dataGridView1_CellFormatting(object sender, 
    DataGridViewCellFormattingEventArgs e)
{
    e.Value = 1;
}

It sets a new value in the cell, but I need to select a value.

Form loaded and I have empty cell.

Form loaded and I have empty cell.

And some data in the ComboBox.

And some data in the ComboBox.

When I put this code dataGridView1.Rows[0].Cells["ComboColumn"].Value = "1"; right after comboBoxCell.DisplayMember = … (see above), it works fine.

The value “1” in the ID column corresponds to the value “Second” in the Items column.So, I get the correct result.

The value "1" in the ID column corresponds to the value "Second" in the Items column.So, I get the correct result.

Sorry for my English and my newbie code 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:50:15+00:00Added an answer on June 8, 2026 at 10:50 am

    Instead of adding a cell to your grid add a DataGridViewComboBox column.

    DataGridViewComboBoxColumn c = new DataGridViewComboBoxColumn();
    c.Name = "ComboColumn";
    c.DataSource = dataTable;
    c.ValueMember = "ID";
    c.DisplayMember = "Item";
    dataGridView1.Columns.Add(c);
    

    To select a particular value you set the Value property of a given cell.

    dataGridView1.Rows[rowIndexYouWant].Cells["ComboColumn"].Value = 1;
    
    • Note that the type here is important! In comments you say you get a System.FormatException. This can be caused by setting the wrong type to the value.

      When you set the value to 1 you are assigning an int – if for some reason you have strings in the ID column you will get the System.FormatException exception you are seeing.

      If the types differ you need to either update the DataTable definition or set the value to a string:

      dataGridView1.Rows[rowIndexYouWant].Cells["ComboColumn"].Value = "1";
      
    • Also note that this value must be present in the ID column of the DataTable that you have set as the source of the grid.

    It is usually easiest to work with a DataGridView when it has its DataSource set. In this case you can bind the ComboBoxColumn to the grid’s DataSource using the DataPropertyName property.

    c.DataPropertyName = "GridDataSourceColumnName";
    

    This allows the columns value to be taken from the grid data source and for changes to the column to directly change that data source.


    Lastly, do not use the CellFormatting event here – this event is not intended for this sort of use. It is usually best to do this sort of work in the DataBindingComplete event (if you only want it done once) or during some event like DefaultValues needed or RowValidating.

    By using CellFormatting you will probably make it impossible for users to manually edit the combo box.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataGridViewComboBoxCell, whose DataSource is a List. When an item is selected
A bunch of dotnet framework components use a DataSource component. I have an object
I have a windows forms DataGridView that contains some DataGridViewComboBoxCell s that are bound
have this code: void set(list<Person*>* listP){ Person timmy = Person(10); listP->push_back(&timmy); } int main()
Hi Programmers, Actually I have a DataGridViewComboBoxCell in DataGridvIew and I need to change
In order to bring up the menu for a DataGridViewComboBoxCell, I first have to
have written this little class, which generates a UUID every time an object of
I have a DataGridView (called DataGridViewSecurity) in VB.net (Visual Studio 2010) which is bound
I have a method that updates a column that contains a type DataGridViewComboBoxCell, early
I want to set the items of each DataGridViewComboBoxCell individually (beacause each combobox must

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.