I have a DataGridView that I am binding to a POCO. I have the databinding working fine. However, I have added a combobox column that I want to be different for each row. Specifically, I have a grid of purchased items, some of which have sizes (like Adult XL, Adult L) and other items are not sized (like Car Magnet.)
So essentially what I want to change is the DATA SOURCE for a combobox column in the data grid. Can that be done?
What event can I hook into that would allow me to change properties of certain columns FOR EACH ROW? An acceptable alternative is to change a property when the user clicks or tabs into the row. What event is that?
Seth
EDIT
I need more help with this question. With Triduses help I am SO close but I need a bit more information.
First, per the question, is the CellFormatting event really the best/only event for changing the DataSource for a combo box column. I ask because I am doing something rather resource/data intensive, not merely formatting the cell.
Second, the cellformatting event is being called just by having the mouse hover over the cell. I tried to set the FormattingApplied property inside my if-block and then I check for it in the if- test but that is returning a weird error message. My ideal situation is that I would apply change the data source for the combo box once for each row and then be done with it.
Finally, in order to set the data source of the combobox colunm I have to be able to cast the Cell inside my if block to a type of DataGridViewComboBoxColumn so that I can fill it with rows or set the datasource or something. Here is the code I have right now.
Private Sub ProductsDataGrid_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles ProductsDataGrid.CellFormatting
If e.ColumnIndex = ProductsDataGrid.Columns("SizeDGColumn").Index Then ' AndAlso Not e.FormattingApplied Then
Dim product As LeagueOrderProductInfo = DirectCast(ProductsDataGrid.Rows(e.RowIndex).DataBoundItem, LeagueOrderProductInfo)
Dim sizes As LeagueOrderProductSizeList = product.ProductSizes
sizes.RemoveSizeFromList(_parentOrderDetail.SizeID)
'WHAT DO I DO HERE TO FILL THE COMBOBOX COLUMN WITH THE sizes collection.
End If
End Sub
Please help. I am completely stuck and this task item should have taken an hour and I am 4+ hours in now. BTW, I am also open to resolving this by taking a completely different direction with it (as long as I can be done quickly.)
Seth
I didn’t go the way of catching an event to change displayed values in a combobox for each row…Take a look at this approach (example), it’s very straightforward and easy to implement and does exactly what you want imho.:
Declare an Item class with two simple properties (Name, Type)
Create a DataGridView with two columns (one for Name, and the second for Type with ColumnType = DataGridViewComboBoxColumn and dataPropertyName = Type)
“Attach” your (I assume) list of poco’s to the DataGridView
And afterwards loop one-time only through your collection of rows; grab the DataGridViewComboBoxColumn for that row; Change it’s items property based upon the criteria that you have for the poco in that row.
I leave it up to you to grab the poco for a specific row, or alternatively read some properties from hard-coded columns in the row…
Here’s some sample code:
The code is in C# but is pretty basic, so I guess it won’t cause problems in VB.NET. And this code really makes a one time loop (O(n)) to configure your combo’s for different rows and than it’s done.
Hope this helps. Joep.