简体   繁体   中英

Winform DataGridView not refreshing after changing datasource programmatically

I have a DataGridView that won't update its contents after programmatically setting it's datasource. I've verified that the new data is fine, and tried .Refresh, setting the datasource to nothing and a few other things to get it to redraw itself with the new data. I've spent hours trying every combination of events (even downgrading the DotNet framework to 3.5 to see if it was a bug in 4.5 framework) and tricks from other forums. Nothing works.

Public Sub GetNewOrders(Optional calledfrompopup As Boolean = False)
    Me.dgvOrders.DataSource = QBI.Order_DataICT.GetNewOrdersICT()
    If calledfrompopup Then
        ''100% certain that the datasource info has changed, yet datagridview just won't reflect the change!
        Me.pnlOrders.Refresh() 'does not work
        Me.dgvOrders.Refresh() 'does not work
    End If
End Sub

Basically, another form that was opened, calls "GetNewOrders" in its form.closing event, then closes. The calling form has the method above and checks to see if it needs to refresh the datagridview (a solution I thought would work, but, alas, it does not). As you can see, I've tried refreshing the panel that the datagridview is in (as well as the form itself). Refresh does absolutely nothing and there is no redraw or rebind or anything I can find that will FORCE the datagridview to reload itself. Stumped! Answers in either C# or VB.Net language are okay.


another version of the function showing other things I've tried

Public Sub GetNewOrders(Optional calledfrompopup As Boolean = False)
    Me.dgvOrders.DataSource = Nothing 'does not work
    Me.dgvOrders.DataMember = "" 'does not work
    Me.dgvOrders.DataSource = QBI.Order_DataICT.GetNewOrdersICT() '100% certain that the datasource info has changed, yet datagridview just won't reflect the change!

    If calledfrompopup Then
        Me.pnlOrders.Refresh() 'does not work
        Me.dgvOrders.Refresh() 'does not work
        Me.dgvOrders.Show() 'does not work
        Me.dgvOrders.Visible = False 'does not work
        Me.dgvOrders.Visible = True 'does not work
    End If
End Sub

Success!! What I figured out was that all I have to do is use ShowDialog() instead of Show() when opening the child form, so that when it closes, I only need to check for DialogResult.Cancel, then call the function GetNewOrders from the parent as in:

   Dim frm2 As New frmSendToQuickbooksPopup
    frm2.CurrentOrder = Order
    frm2.lineitems = OrdLineItems
    frm2.payments = OrdPayments
    Dim diaResult As DialogResult = frm2.ShowDialog() 
    If diaResult = Windows.Forms.DialogResult.Cancel Then
        GetNewOrders()
    End If

To explain: frm2 is opened and when it is closed, the datagridview (which is on the parent form) now sees that it is closed and calls the method GetNewOrders().

You are refreshing inside of an if statement:

If calledfrompopup Then

Make sure you are passing in true and that you are making it in there. Next, instead of refreshing just set the data source inside of that if statement:

If calledfrompopup Then
    //The use of "Me" here may not be necessary.
    Me.dgvOrders.DataSource = QBI.Order_DataICT.GetNewOrdersICT()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM