Setting a DataGridViewComboboxCell value

atnpowell

New member
Joined
Mar 15, 2007
Messages
3
Programming Experience
3-5
On my DataGridView I am adding a DataGridViewComboBoxColumn with a dataview as its datasource. This all loads up fine but I want to be able set a particular value to be visible as the selected item straight away when the grid is loaded and not have to click on the combobox to select one.
How do I either set a selected value for each combo box cell OR programmatically select a particular value as the one to be displayed after the grid has loaded ?

cheers

Andy
 
Thanks for the reply ... I had already looked at the posts you reccomended. These however only show how to set a single default value for an entire DataGridViewComboBoxColumn whereas I need to set different values in each separate DataGridViewComboBoxCell.

Basically I am binding a datatable containing 3 "Reason" values to the DataGridViewComboBoxColumn. For each row in the grid I then check a column called "ReasonID" which holds the actual value that needs to be set as default in the combobox on the same row.

VB.NET:
Dim i AsInteger = 0
Dim objCombo As DataGridViewComboBoxCell
Dim drv As DataRowView
 
For Each objRow In objDataGrid.Rows

[INDENT]'Get our default combox value for this row[/INDENT][INDENT]strReason = objRow.Cells("ReasonID").Value.ToString[/INDENT][INDENT]objCombo = DirectCast(objRow.Cells("Reason"),DataGridViewComboBoxCell)[/INDENT][INDENT]i=0[/INDENT][INDENT]'loop through the ComboBoxCell values[/INDENT][INDENT]For Each objItem AsObject In objCombo.Items[INDENT]drv = DirectCast(objItem, DataRowView)[/INDENT][INDENT]'check whether our ComboBoxCell value matches our required value[/INDENT][INDENT]If drv.Item(0).ToString = strReason Then[/INDENT]

[INDENT][INDENT]'we have a match .. so apply it to the ComboBoxCell [/INDENT][INDENT]objCombo.Value = objCombo.Items(i).Item(1).ToString[/INDENT]Exit For
EndIf
 

[/INDENT]i += 1
 

[/INDENT]Next
Next

The line
VB.NET:
 objCombo.Value = objCombo.Items(i).Item(1).ToString
obviously doesn;t work, but how can I set each ComboBoxCell value ?
 
Back
Top