Question In Datagridview, Add Items to ComboBox Column at run time

prasad kulkarni

Well-known member
Joined
Sep 7, 2009
Messages
62
Programming Experience
1-3
Hi,

I add ComboBox column at design mode and I am trying to add Items in
ComboBox at run time.
How to add Items to ComboBox at form load

if any know, then reply me soon
 
How do you add items at design time? You add values to the column's Items property.
How do you add items at run time? You add values to the column's Items property.
 
Populating the drop-down list of a DataGridViewComboBoxColumn is no different to populating the drop-down list of a ComboBox control. They both have an Items property to add values directly and they both have a DataSource property to bind a list of items. They both also have DisplayMember and ValueMember properties.

If you can populate a ComboBox then you can populate a DataGridViewComboBoxColumn. If you can't populate a ComboBox then you should read the MSDN documentation for the ComboBox class; particularly for the properties I mentioned above. If you read that and try to do the job but can't, then you should post here and show us what you've done and we can help you fix it.
 
Acually what i am trying to do is that

I have 3 items in datagridview

1. course
2. venue
3. schedule

all 3 r combo's

on selection of course i have to add values to
venue combobox in datagridview.

on selection of venue add values to
schedule combobox

This is code i am using
---------------------------------------------
Private Sub mygridview_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles mygridview.CellValueChanged
Try
Dim ColumnIndex, RowIndex, RowValue
ColumnIndex = mygridview.CurrentCell.ColumnIndex
'MsgBox(ColumnIndex)
RowIndex = CStr(mygridview.CurrentCell.RowIndex)
RowValue = CStr(mygridview.CurrentCell.Value)
MsgBox(RowIndex + RowValue)
'Dim s As String = CStr(mygridview.Rows(RowIndex).Cells("course").Value)
s = "select distinct venue from cvs1 where course='" + RowValue + "' "
'MsgBox(s)
''and enrolno ='" + txtenrolno.Text + "'"
cmd = New System.Data.OleDb.OleDbCommand(s, cn)
rs = cmd.ExecuteReader()
While (rs.Read())
MsgBox(rs.GetValue(0))
' mygridview.Item(1, 0) = rs.GetValue(0)
mygridview.Rows(RowIndex).Cells(1).Value() = rs.GetValue(0)
'mygridview.Item(mygridview(1, cnt), ColumnIndex + 1) = rs(0)
'MsgBox(a)
'mygridview.Rows(RowIndex).Cells("venue") = rs(0)
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

-----------------------------
Please do correct the code & answer to above
 
Acually what i am trying to do is that

I have 3 items in datagridview

1. course
2. venue
3. schedule

all 3 r combo's

on selection of course i have to add values to
venue combobox in datagridview.

on selection of venue add values to
schedule combobox
That's a different situation. In that case you want different values in the drop-down list for each row, so you can't set the values for the column as a whole. You have to wait until the user actually starts editing one of the cells. At that point you get a reference to the ComboBox control embedded in that cell and add the appropriate items to the drop-down list for that specific cell. The steps are:

1. Handle the EditingControlShowing event of the grid, which is raised when the user starts editing a cell and a control is embedded to facilitate that.

2. Determine which column the control is in so that you know what you actually have to do.

3. Determin what row the control is in.

4. Get the data from that row that will tell you what you need to display in the drop-down list.

5. Get a reference to the embedded control and cast as type ComboBox.

6. Add the appropriate items to the list.

You can get most of the information you need from the 'e' parameter of the event handler.

Note also that you should almost certainly clear any dependent values when the user sets a cell value.
 
Back
Top