Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi. I have a pair of Master-Detail DataGridViews in my WinForms App.
One of the columns in my DETAIL grid is a ComboBox (part_name).

ComboBox values are not the same for all rows. They differ based on the value of another column (equipment_name) in the same row of the DETAIL grid.
example:

row1: equipment_name: PC part_name: RAM , Hard disc , CPU
row2: equipment_name: printer part_name: paper tray , Inkwell

How can i populate each ComboBox with correct values in each DETAIL row when detail rows are either filled from database or added by user? maybe using a database query? how to pass(equipment_name) as a parameter? and where to do that? in what event handler?


P.S. :
1- master and detail grids are used for displaying, adding , editing and deleting rows.
2- in this page you can find my code of creating grids. it doesn't include code for ComboBox column yet. the correct code is marked as an ANSWER.

www.codeproject.com/Questions/817587/Master-detail-datagridview-how-to-insert-master-re?arn=0&loginkey=false[^]

thanks in advance
Posted
Updated 13-Oct-14 1:02am
v6

1 solution

Use CellFormatting event to check for equipment name and based on that set datasource for the combobox column.

You can also use DataBindingComplete, but you have to then iterate through whole grid and re-bind your combobox values which is less than ideal.

In CellFormatting, check cell index or cellcolumn name, if it is your equipment name, bind your datasource to combobox column
 
Share this answer
 
v2
Comments
nina4ever 13-Oct-14 7:01am    
thanks, but it's a Windows Forms Application. there is no such event
Sinisa Hajnal 13-Oct-14 7:18am    
Stupid of me. Sorry. Solution updated.
nina4ever 13-Oct-14 7:33am    
thank you again. i will try your solution and feedback.
nina4ever 13-Oct-14 7:47am    
"CellFormatting event occurs every time each cell is painted". is it practical to query database for combo values each time this handler executed !!!?
Sinisa Hajnal 13-Oct-14 7:57am    
It's not. It is up to you to limit it to specific cell (that is, use cell index or cell column to check that it is equipment part cell and THEN and ONLY THEN query the database :)

You can also improve performance by getting both combobox datasources on form load and then only assigning them to comboboxes...just take care to copy the source instead of assigning directly (datatable.Copy is enough assuming you have DataTable as combo source)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900