Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridViewComboBoxColumn - populate existing combobox with items.add
 

DataGridViewComboBoxColumn - populate existing combobox with items.add

Using VB and trying to do a simple items collection add. Cannot seem to properly reference the designer created

DataGridViewComboBoxColumn.

My code will build a new one and add it just fine, but when I try to declare a reference to the existing one and add items I get a nullreference exception. Here is the dim:

Dim comboboxColumn As New DataGridViewComboBoxColumn = Ci_FormsDataGridView.Columns("formname")

removing New does not help
DevDells  Monday, December 17, 2007 12:53 PM

Sorry about that; I messed up on the answer. I did some testing and I think this will give you what you want.

Code Block

/*dataGridView1 contains 1 DataGridViewComboBoxCell

* atrow index0 column index0

*/

/* Get theComboBox Cell*/

DataGridViewComboBoxCell cBoxCell =

this.dataGridView1.Rows[0].Cells[0] as DataGridViewComboBoxCell;

/*Check if we got the cell correctly*/

if (cBoxCell != null)

{

/* Add new item*/

cBoxCell.Items.Add("New Item");

}

Let me know if this works or if its unclear.

Best

-Tom

pot8oe  Wednesday, December 19, 2007 4:55 PM

Hi,

I dont write in VB but would this work? I'm not sure if you would want the new and I'm assuming As works the same in VB as it does in C#.

Code Block

Dim comboboxColumn As New DataGridViewComboBoxColumn = Ci_FormsDataGridView.Columns("formname") As DataGridViewComboBoxColumn

// if the combo does not equal null do your logic

Is there any reason you do not work directly on the column from the grid view? Also if you want to access the comboBoxes in the column you'll probably want to go thourgh the rows property.

Code Block

Dim rowIndex As int 0

Ci_FormsDataGridView.Rows[rowIndex]["columnname"]

pot8oe  Monday, December 17, 2007 3:42 PM

Sorry, this does not seem to address my question. Let me be clearer.

I can get a completely new combobox to drop into the datagridview. And it's nicely loaded with values aftrer using items.add.

But, what I cannot do is referenence the existing designer created combobox in the grid in order to populate it using items.add.

DevDells  Wednesday, December 19, 2007 3:16 PM

Sorry about that; I messed up on the answer. I did some testing and I think this will give you what you want.

Code Block

/*dataGridView1 contains 1 DataGridViewComboBoxCell

* atrow index0 column index0

*/

/* Get theComboBox Cell*/

DataGridViewComboBoxCell cBoxCell =

this.dataGridView1.Rows[0].Cells[0] as DataGridViewComboBoxCell;

/*Check if we got the cell correctly*/

if (cBoxCell != null)

{

/* Add new item*/

cBoxCell.Items.Add("New Item");

}

Let me know if this works or if its unclear.

Best

-Tom

pot8oe  Wednesday, December 19, 2007 4:55 PM

Hello.
I found this on an other forum that help me.

It will get you a reference tothe selected DataGridViewComboBoxCell.
In my example I have two comboboxes in my grid andwhen the user changes the value in the first oneit will

filter the data in the next one so only related data is shown. (didnt paste the whole example)

You have toknow whichcellyou want to reference and you do that by e.columnindex and e.rowindex.

Good luck./P

Private Sub grdAvtal_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdAvtal.CellValueChanged

If grdAvtal.Columns(e.ColumnIndex).Name = "iVE_Id_Avtalspart" Then

Dim cboCellProdEnhet As DataGridViewComboBoxCell

cboCellProdEnhet = CType(grdAvtal(e.ColumnIndex + 1, e.RowIndex), DataGridViewComboBoxCell)

...

...
pelle sunkan  Thursday, December 20, 2007 6:58 PM

Thanks for this info. Could you post the link to the other forum for complete code?

Also, I think I'm trying to do something different than you show here. Although, in the end I want the same result. I want to be able to select a value in the first datagridcombobox and have it then provide a subset in the second datagridviewcombobox.

I tried to start with a datagridview control that already has two datagridviewcombobox columns. But I want to populate them by setting their datasource property dynamically at runtime. I can of course use the dataset/tableadapter/bindingsource approach, but I was trying for something more lightweight than that.

So, I retrieve a dataresultset as shown below and I set the datagridviewcombobox properties in some event (say Form.Load event for example). This code will work fine with a comboboxcontrol outside of the grid. But when it is set up as I have it below, it fails to populate the combobox in the grid.

Code Block

Using connection As New SqlCeConnection(My.Settings.dbLookup)

Dim comboboxColumn As DataGridViewComboBoxColumn = DataGridView1.Columns.Item("sectionNumber")

Dim command As SqlCeCommand = New SqlCeCommand("SELECT secNum, secNumDescFROM lookupNumbers", connection)

connection.Open()

Dim catResult As SqlCeResultSet = command.ExecuteResultSet(ResultSetOptions.Scrollable)

comboboxColumn.DataSource = catResult

comboboxColumn.ValueMember = "secNum"

comboboxColumn.DisplayMember = "secNumDesc"

End Using

This will return an error "No data exists for the row/column"

Same error occurs if I build a new datagridviewcomboboxcolumn and then do a datagridview1.columns.add

I don't want to build the combobox with Items.add since the datasource will have two columsn, one display and one value.

I guess my major disconnect here is trying to treat the datagridviewcombobox as a standard form combobox control. And maybe not understanding the event model of the datagridview well enough to understand the populating of said column.

DevDells  Friday, December 28, 2007 2:34 PM

You can use google to search for other answers

Custom Search

More Threads

• DataView sorting and rebinding
• subclassed DataGridView OnCellValueChanged weird stuff
• populating grid on the fly
• Sorting ListView Data
• DataGridView: New row dissapears
• ComboxColumn in DataGridView (VS.NET 2005) not support AutoCompleteMode, DropDowStyle
• New ASP 2.0 controls - problems accessing controls on templates
• vb will write record to sql one time and not the next
• Datagridview DatagridviewColumnComboBox
• How to bind a custom business object that contains other business objects?