Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamicly created DataGridView initialization issue

Status
Not open for further replies.

JScannell

Programmer
Jan 9, 2001
306
US
I have a Windows Form that has a Panel on it. A dynamically generated DataGridView control gets added to the Panel. One of the columns in the DGV is a combobox. It is populated with all possible Bottle Types when the DGV is created. Its initial value gets set to the value of one of the hidden columns. The form is called from a menu strip action. So it can be called over and over depending on what the user is doing.

The very first time the form is loaded, the combobox's value gets updated correctly. If I close the form and reopen it again, something is wrong and the combobox's values don't appear as updated.

Please see the attached document.

Of particular note, the very first time the form is loaded, the DGV CellEnter and CellLeave events don't fire unless you actually click into a column. But on subsequent form loads, both events fire even though I'm not entering or leaving any of the cells. I believe that might be a clue as to what's going on, but I can't prove or verify it. There is definitely something odd happening.

Without clouding the issue with all of my code, I will share the combobox column creation as well as the post-generating Value setting.

1. Inserting a combobox column. Based on a special object of type "AddOn" which is in addition to the columns retrieved from the SQL query:
Dim oCombo As New DataGridViewComboBoxColumn
oCombo.DataSource = oAddon.dgvCmbDatasource
oCombo.Width = oAddon.dgvWidth

oCombo.DisplayMember = "Display"
oCombo.ValueMember = "Value"

oDGV.Columns.Insert ( oAddon.dgvColumnNumber, oCombo )

oDGV.Columns( oAddon.dgvColumnNumber ).Name = oAddon.dgvColumnName ' "BottleType"
oDGV.Columns( oAddon.dgvColumnNumber ).HeaderText = oAddon.dgvHeaderText
oDGV.Columns( oAddon.dgvColumnNumber ).HeaderCell.Style.Alignment = oAddon.dgvHeaderAlignment
oDGV.Columns( oAddon.dgvColumnNumber ).Width = oAddon.dgvWidth
oDGV.Columns( oAddon.dgvColumnNumber ).ReadOnly = false

numCurrentWidth = oDGV.Size.Width
oDGV.Size = New Size ( numCurrentWidth + oAddon.dgvWidth, numHeight )

2. Populating the Value properety after creation:
For Each row As DataGridViewRow In objDGV.oDGV.Rows ' objDGV.oDGV
If Not row.IsNewRow Then
'
' Transfer the current bottle type ID to the combobox
'
' Sanity check: Get the current Bottle ID value
Dim strOld_ID As String = row.Cells ( "BottleType_ID" ).Value

' Change the Combobox value to match the current ID
row.Cells( "BottleType" ).Value = strOld_ID.ToString()

' Sanity check proof: Get the changed ID.
Dim strNew_ID As String = row.Cells ( "BottleType" ).Value

objDGV.oDGV.Refresh()
End if
Next

Hopefully someone has come across this type of problem and can shed some light.

Thanks in advance,
Jerry

Jerry Scannell
 
 https://files.engineering.com/getfile.aspx?folder=be2bae36-e7f5-4ad5-8d10-b1828e5dcdfb&file=DataGridView_Anomaly.pdf
Do you recreate the grid every time the menu strip is clicked, or do you create it once then reuse it?



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Yes. It is dynamically created and added to the Panel every time the form is opened. I've tried it by keeping the object open, but that didn't work because the form is closed when the user Exits it.

I had to do a klugey thing as a workaround. I discovered that if I initiated a cell enter followed by a leave, then the combobox column would upddate correctly magically. So what I did was create a "show" event for the form where I force a cell enter/leave scenario and the DGV would show correctly. What a kluge, though.

I would think that there shouldn't be anything different from creating the control initially and then subsequently since the object was nothing to begin with each time.

Jerry

Code:

'
' This is needed so the combobox column initial values will be set. It is necessary to force some CellLeave events to make the DGV display updated
'
Private Sub deAirBottleInventory_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
objDGV.odgv.CurrentCell = objDGV.odgv.Rows(0).Cells(3)
objDGV.odgv.CurrentCell = objDGV.odgv.Rows(0).Cells(2)

flgDidRedraw = True
End Sub

Then in the dgv_CellLeave event:

'
' Try to set the values of any added comboboxes...
'
If Not flgDidRedraw Then
For each oAddon As dgvSetup.DATAGRID_Addon In objDGV.GridAddons
'
' Set the initial value of the combobox
'
If Not String.IsNullOrEmpty( oAddOn.dgvValueColumn ) Then
'
' Loop through the entire grid...
'
For Each row As DataGridViewRow In dgv.Rows
Dim curValue As String = row.Cells( oAddon.dgvValueColumn ).Value

row.Cells( oAddOn.dgvColumnNumber ).Value = curValue
Next

End If

dgv.Update ()
dgv.Refresh ()
Next
End If



Jerry Scannell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top