未绑定的DataGridView未显示添加的行

时间:2017-02-07 16:06:49

标签: vb.net winforms datagridview

我有一个VB应用程序,我正在使用DataGridViews来显示各种类型的数据。该应用程序具有单独的表单,以便为DataGridViews中显示的每个表创建新记录。数据传递正常工作(应用程序在关闭时将数据存储在平面文件中,并且具有应有的一切)。

我最近从Visible_Changed事件中提取了一些代码,该事件处理将数据从条目表单上的字段传递到显示器。这是完全相同的代码,只是由报名表格专门调用。在显示窗体上保存按钮而不是Visible_Changed事件(这会在调用和创建变量需要时阻止执行某些代码,这取决于窗体变为可见/不可见的原因)。

在更改之后,数据似乎仍然正确传递(我有Checked_List_Views作为DataGridView的过滤器,他们正在查看新项目),但该项目没有显示。我以前在旧代码中遇到了这个问题,并通过添加Msr_List.Refresh来强制网格刷新来解决它。尽管如此,这似乎并没有产生同样的影响。有关如何让DataGridView实际更新以包含新项目的任何想法将不胜感激。

这是旧代码:

Private Sub Form_Visibility_Change(sender As Object, e As EventArgs) Handles MyBase.VisibleChanged
    '
    Dim Sort_Col As DataGridViewColumn = Nothing
    Dim Need_Statuses As Boolean = True

    'Check if Add_Edit_Status is blank, if so no action needed
    Select Case UCase(New_Edit_Status)

        'Determine what form we just came back from (NOTE: The Create Statuses button doesn't leave this form so this Sub isn't invoked by it, thus that case isn't listed)
        Case "", "CANCEL", "OUTBOUND"
            'Do nothing
        Case "SAVE-NEW_MSR"
            'How to proceed?
            If Not IsNothing(Target_Row) Then
                'Prevent creation of bogus status records
                Need_Statuses = False
                Try
                    'Delete the current version. This way the normal Add step covers both cases
                    Msr_List.Rows.Remove(Target_Row)
                    Target_Row = Nothing
                Catch ex As Exception
                    MsgBox("Encountered an error while removing the old version of a measure. Due to the potentially dire consequences of this error, the program" _
                           & " will now close. The HEDIS Measure XML should be restored from backup. Please see the error log for more details.", _
                           vbOKOnly, "Critical Measure List Management Error")
                    MWin.Err.AddErr("Encountered an error while removing the old version of a measure that was updated. Details: " & ex.Message _
                                    & Chr(10) & Chr(10) & ex.InnerException.Message, MWin.Err_Path & MWin.Err.DStamp & MWin.Log_Name)
                    MWin.Close()
                End Try
            End If 'else we're adding a new population so just proceed to adding the list row

            'Add the new row/version of the row
            Try
                Msr_List.Rows.Add(New_Msr_Form.Msr_ID.Text, New_Msr_Form.Msr_Name.Text, New_Msr_Form.SubMsr_Name.Text, New_Msr_Form.Msr_Key.Text, _
                                  New_Msr_Form.SubMsr_Key.Text, New_Msr_Form.Spec_Section.Text, New_Msr_Form.Complexity.Text, New_Msr_Form.Spec_Yr_Created.Text, _
                                  New_Msr_Form.Spec_Yr_Removed.Text, New_Msr_Form.Denom_Only.Checked, New_Msr_Form.Report_Medicaid.Checked, _
                                  New_Msr_Form.Report_Medicare.Checked, New_Msr_Form.Report_Ambetter.Checked)

                Msr_List.Refresh()
                Refresh()
                'Update form variables
                New_Edit_Status = ""
                Msrs_Changed = True
            Catch ex As Exception
                MsgBox("Encountered an error while creating the new version of an update measure. Due to the potentially dire consequences of this" _
                       & "error, the program will now close. The HEDIS Measure XML should be restored from backup. Please see the error log for " _
                       & "more details.", vbOKOnly, "Updated Measure Creation Error")
                MWin.Err.AddErr("Encountered an error while adding a new row to the QSI Populations DataGridView. Details: " & ex.Message, _
                                MWin.Err_Path & MWin.Err.DStamp & MWin.Log_Name)
                MWin.Close()
            End Try

            'Push the data back to the data table
            Fill_Tbl_From_List(Msr_List, MWin.HEDIS_Msr_List_Tbl, "Msr_List_View", MWin)

            'Create statuses if needed
            If Need_Statuses Then
                Create_Statuses(New_Msr_Form.Spec_Yr_Created.Text, New_Msr_Form.Msr_ID.Text, MWin)
            End If 'else nothing needs done

            'Re-sort
            Try
                Sort_Col = Msr_List.Columns("Msr_ID")
                Msr_List.Sort(Sort_Col, System.ComponentModel.ListSortDirection.Ascending)

            Catch ex As Exception
                MsgBox("Encountered an error trying re-sort the updated HEDIS Measures list to properly place the new/updated row. This is" _
                       & " non-critical so the program will continue, but an error log entry has been created with more details.", vbOKOnly, _
                       "Measure List Sort Error")
                MWin.Err.AddErr("Encountered an error while trying to sort the QSI Populations DataGridView. Details: " & ex.Message & _
                                Chr(10) & Chr(10) & "Inner Exception: " & ex.InnerException.Message, MWin.Err_Path & MWin.Err.DStamp & MWin.Log_Name)
            End Try

        Case "SAVE-NEW_BENCH"
            'Create the record in the benchmark history data table
            MWin.Benchmarking_Hist.Rows.Add(New_Bench.Msr_ID.Text, New_Bench.Population.Text, New_Bench.Spec_Yr.Text)

            'Reset New_Edit_Status
            New_Edit_Status = ""
            New_Bench = Nothing

        Case Else
            MsgBox("Encountered error while handling the HEDIS Measure List Viewer becoming visible. A bad New_Edit_Status was passed, specifically '" & _
                   New_Edit_Status & "'. This will mean some data may be lost & the form will act as though it's opening from the Main Window.", vbOKOnly, _
                   "Unknown New_Edit_Status")
            MWin.Err.AddErr("Unhandled return from QSI Add/Edit form to QSI Population Viewer form of '" & New_Edit_Status & _
                            "'. Please address this return value.", MWin.Err_Path & MWin.Err.DStamp & MWin.Log_Name)
            New_Edit_Status = ""

    End Select

    'Check if we should enable the Edit btn
    If Msr_List.Rows.Count > 0 Then
        Edit_Btn.Enabled = True
    Else
        Edit_Btn.Enabled = False
    End If

    'Dispose of the form
    If Not (IsNothing(New_Msr_Form)) And New_Edit_Status <> "Outbound" Then
        New_Msr_Form = Nothing
    End If 'else no form to dispose of
End Sub

Msr_List是DataGridView,New_Msr_Form是数据输入表单,Target_Row是DataGridView的选定行(如果有的话,仅限于选择一行),{{1}我是一个笨重的方式让显示形式知道新条目是否成功。显然不理想,但(大部分)工作。

这是新代码:

New_Edit_Status

谢谢!

0 个答案:

没有答案