添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

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.

Sorry for my English and my newbie code :)

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.

    I did as you said, but I still get an exception: System.FormatException: an invalid value DataGridViewCombobBoxCell. @David Hall – Dima Jul 25, 2012 at 20:04 @Stepler don't set the value during the formatting event (This will probably make it impossible to change the value manually or do something equally weird) - it is usually best to do in the databindingcomplete event. Does your grid have a datasource? And are you sure that the value you are setting is in the datatable id column? – David Hall Jul 25, 2012 at 20:09 @Stepler although I just tried this in the CellFormatting event and it works unless the value you set does not match the datatable. you need to ensure that the value you set (in this case 1) is a value from your datatable id column. – David Hall Jul 25, 2012 at 20:12 >Does your grid have a datasource? - Yes, I have some items in ComboBox (I add pics in my question) >And are you sure that the value you are setting is in the datatable id column? - Yes. I have 0, 1 and 2 in ID column. – Dima Jul 25, 2012 at 20:30 Yippee! dataGridView1.Rows[rowIndexYouWant].Cells["ComboColumn"].Value = "1"; works. All the matter "1". Thank you for your time and detailed response. – Dima Jul 25, 2012 at 20:36

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •