Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Dynamicly created DataGridView initialization issue

Dynamicly created DataGridView initialization issue

Dynamicly created DataGridView initialization issue

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

End if

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

Thanks in advance,

Jerry Scannell

RE: Dynamicly created DataGridView initialization issue

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!

RE: Dynamicly created DataGridView initialization issue

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.



' 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

End If

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

Jerry Scannell

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close