从form2编辑后,form1中的DataGridview不会更新(使用语句,Try Catch语句) [英] DataGridview in form1 not update after edit from form2 (Using statement, Try Catch statement)

查看:42
本文介绍了从form2编辑后,form1中的DataGridview不会更新(使用语句,Try Catch语句)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello 


我刚刚更改为使用using语句,当我需要在form1的DataGridview中更新数据时出现问题。


我的步骤是> open form1(数据列表)>选择数据并在form2上选择打开(编辑表格)>在form2上编辑数据并保存>然后刷新form1上的数据,但数据不会改变。


这是我的代码 


Form1:  customer_analyzer_list  (加载数据工作正常)

 Public Sub LoadCustAnalyzerList()
尝试
使用StrWer作为新的StreamReader(Application.StartupPath& " \ AppConn.ini")
使用objConn作为新的MySqlConnection(StrWer.ReadLine)
dtCtmAnl.Clear()
objConn.Open()
如果tbKeySearch.Text = ""然后
StrSql =" SELECT A.customer_analyzer_list_id,B.hos_name,A.analyzer_name,A.install_date,A.cancel_date,A.install_staff,CASE A.active WHEN TRUE THEN 1 ELSE 0 END as CALActive,A。 po_no,A.inv_no" &安培;
" FROM customer_analyzer_list A LEFT JOIN hos_list B ON A.hos_code = B.hos_code LIMIT 100;"
Else
StrSql =" SELECT A.customer_analyzer_list_id,B.hos_name,A.analyzer_name,A.install_date,A.cancel_date,A.install_staff,CASE A.active WHEN TRUE THEN 1 ELSE 0 END AS CALActive,A.po_no,A.inv_no" &安培;
" FROM customer_analyzer_list A LEFT JOIN hos_list B ON A.hos_code = B.hos_code WHERE B.hos_name LIKE'%" &安培; tbKeySearch.Text& "%'或A.analyzer_name LIKE'%" &安培; tbKeySearch.Text& "%";"
结束如果
使用objCmd作为新的MySqlCommand(StrSql,objConn)
Dim adapter As New MySqlDataAdapter(objCmd)
adapter.Fill(dtCtmAnl)

如果dtCtmAnl.Rows.Count> 0然后
gvCALData.DataSource = dtCtmAnl
gvCALData.Columns(0).Visible = False
gvCALData.Columns(1).HeaderText ="โรงพยาบาล"
gvCALData.Columns(1).Width = 250
gvCALData.Columns(2).HeaderText ="ชื่อAnalyzer"
gvCALData.Columns(3).HeaderText ="วันที่ติดตั้ง"
gvCALData.Columns(4).HeaderText ="วันที่ยกเลิก"
gvCALData.Columns(5).HeaderText ="เจ้าหน้าท่่ติดตั้ง"
gvCALData.Columns(6).HeaderText ="สถานะ"
gvCALData.Columns(7).HeaderText ="เลขที่PO"
gvCALData.Columns(8).HeaderText ="เลขที่Invoice"
Else
gvCALData.DataSource = Nothing
End if
End using
End using
End using
Catch ex As Exception
抛出
结束尝试
结束子




表格2:  ; customer_analyzer_edit(编辑表单)

 Private Sub btSaveAnz_Click(sender As Object,e As EventArgs)处理btSaveAnz.Click 
尝试
如果ChkHosCode( tbHosCode.Text.Trim)= 0然后
MessageBox.Show("รหัสโรงพยาบาลไม่ถูกต้อง")
ElseIf cboAnalyzer.SelectedValue = 0然后
MessageBox.Show(&กรุณา) เลือกAnalyzer")
Else
如果rbtStsYes.Checked = True那么
tmpActive = 1
StrSql =" UPDATE customer_analyzer_list SET hos_code ='" &安培; tbHosCode.Text& "",analyzer_id = QUOT; &安培; Convert.ToInt32(cboAnalyzer.SelectedValue)& ",analyzer_name ="" &安培; cboAnalyzer.Text& "",INSTALL_DATE ="" &安培; dtpInstallDate.Value.ToString(" yyyy-M-d",CultureInfo.CreateSpecificCulture(" en-GB"))& "",install_staff ="" &安培; tbStaff.Text& "",活性= QUOT; &安培; tmpActive& ",po_no ="" &安培; tbPONo.Text& "",inv_no ="" &安培; tbInvNo.Text& "'WHERE customer_analyzer_list_id =" &安培; pCALID&英寸;"
Else
tmpActive = 0
StrSql =" UPDATE customer_analyzer_list SET hos_code ='" &安培; tbHosCode.Text& "",analyzer_id = QUOT; &安培; Convert.ToInt32(cboAnalyzer.SelectedValue)& ",analyzer_name ="" &安培; cboAnalyzer.Text& "",INSTALL_DATE ="" &安培; dtpInstallDate.Value.ToString(" yyyy-M-d",CultureInfo.CreateSpecificCulture(" en-GB"))& "",CANCEL_DATE ="" &安培; dtpCancelDate.Value.ToString(" yyyy-M-d",CultureInfo.CreateSpecificCulture(" en-GB"))& "",install_staff ="" &安培; tbStaff.Text& "",活性= QUOT; &安培; tmpActive& ",po_no ="" &安培; tbPONo.Text& "",inv_no ="" &安培; tbInvNo.Text& "'WHERE customer_analyzer_list_id =" &安培; pCALID&英寸;"
结束如果

使用StrWer作为新的StreamReader(Application.StartupPath&" \ AppConn.ini")
使用objConn作为新的MySqlConnection(StrWer.ReadLine)
objConn.Open()
使用trans作为MySqlTransaction = objConn.BeginTransaction
使用objCmd作为新的MySqlCommand(StrSql,objConn)
RowExc = objCmd.ExecuteNonQuery
如果RowExc> 0然后
trans.Commit()
'customer_analyzer_list.btSearchCAL.PerformClick()
调用customer_analyzer_list.LoadCustAnalyzerList()
MessageBox.Show("แก้ไขเรียบร้อย")
'致电customer_analyzer_list.btSearchCAL_Click(发件人,e)
Me.Close()
否则
trans.Rollback()
MessageBox.Show(&แก้ไขไม่สำเร็จ" )
结束如果
结束使用
结束使用
结束使用
结束使用
结束如果
Catch ex As Exception
Mess ageBox.Show(ex.Message.ToString)
结束尝试
结束子



 Private Sub btClose_Click(sender As Object,e As EventArgs)处理btClose.Click 
尝试
'调用customer_analyzer_list.LoadCustAnalyzerList()
'customer_analyzer_list.btSearchCAL .PerformClick()
Me.Close()
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
End Sub

加载数据>打开编辑表单>编辑数据:工作正常但DataGridview中的数据在&customer> ananzerzer_list 没变。 LOL


我试图更改呼叫加载数据行的位置但不起作用。


*调用加载数据和buttonsearch.performclick两者都不兼容。 / p>

 我缺少什么或使用try catch语句的任何示例,有什么建议?


谢谢。


解决方案


*调用加载数据和buttonsearch.performclick两者都不兼容。


 我缺少什么或使用try catch语句的任何示例,有什么建议?


这是您希望更新DGV的电话吗?

   调用customer_analyzer_list.LoadCustAnalyzerList()


您还没有显示任何代码将" customer_analyzer_list"的值设置为包含您的DGV的列表表单的实例想要更新。    通常,此值将根据您将
传递给编辑表单的构造函数的参数进行设置。 如果您依赖此变量的默认值,那将是不可靠的。 您应该在列表表单中调用的方法的开头插入一个断点,并确保代码达到
那一点。 如果不是,则可能的问题是您是否引用了列表表单的错误实例。 


如果您证明您指的是正确的列表表单实例然后您需要确保在更改数据源后刷新DGV。 请参阅:
$
https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.resetbindings%28v=vs.110%29.aspx






Hello 

I just changed to use using statement and I got a problem when I need to update data in DataGridview at form1.

My step is > open form1 (data list) > select data and open selected on form2 (edit form) > Edit data on form2 and save > then refresh data on form1 but data is not change.

this is my code 

Form1: customer_analyzer_list  (Load data working fine)

Public Sub LoadCustAnalyzerList()
        Try
            Using StrWer As New StreamReader(Application.StartupPath & "\AppConn.ini")
                Using objConn As New MySqlConnection(StrWer.ReadLine)
                    dtCtmAnl.Clear()
                    objConn.Open()
                    If tbKeySearch.Text = "" Then
                        StrSql = "SELECT A.customer_analyzer_list_id,B.hos_name,A.analyzer_name,A.install_date,A.cancel_date,A.install_staff,CASE A.active WHEN TRUE THEN 1 ELSE 0 END AS CALActive,A.po_no,A.inv_no " &
                                 "FROM customer_analyzer_list A LEFT JOIN hos_list B ON A.hos_code=B.hos_code LIMIT 100;"
                    Else
                        StrSql = "SELECT A.customer_analyzer_list_id,B.hos_name,A.analyzer_name,A.install_date,A.cancel_date,A.install_staff,CASE A.active WHEN TRUE THEN 1 ELSE 0 END AS CALActive,A.po_no,A.inv_no " &
                                 "FROM customer_analyzer_list A LEFT JOIN hos_list B ON A.hos_code=B.hos_code WHERE B.hos_name LIKE '%" & tbKeySearch.Text & "%' OR A.analyzer_name LIKE '%" & tbKeySearch.Text & "%';"
                    End If
                    Using objCmd As New MySqlCommand(StrSql, objConn)
                        Dim adapter As New MySqlDataAdapter(objCmd)
                        adapter.Fill(dtCtmAnl)

                        If dtCtmAnl.Rows.Count > 0 Then
                            gvCALData.DataSource = dtCtmAnl
                            gvCALData.Columns(0).Visible = False
                            gvCALData.Columns(1).HeaderText = "โรงพยาบาล"
                            gvCALData.Columns(1).Width = 250
                            gvCALData.Columns(2).HeaderText = "ชื่อ Analyzer"
                            gvCALData.Columns(3).HeaderText = "วันที่ติดตั้ง"
                            gvCALData.Columns(4).HeaderText = "วันที่ยกเลิก"
                            gvCALData.Columns(5).HeaderText = "เจ้าหน้าที่ติดตั้ง"
                            gvCALData.Columns(6).HeaderText = "สถานะ"
                            gvCALData.Columns(7).HeaderText = "เลขที่ PO"
                            gvCALData.Columns(8).HeaderText = "เลขที่ Invoice"
                        Else
                            gvCALData.DataSource = Nothing
                        End If
                    End Using
                End Using
            End Using
        Catch ex As Exception
            Throw ex
        End Try
    End Sub


Form2: customer_analyzer_edit (Edit form)

 Private Sub btSaveAnz_Click(sender As Object, e As EventArgs) Handles btSaveAnz.Click
        Try
            If ChkHosCode(tbHosCode.Text.Trim) = 0 Then
                MessageBox.Show("รหัสโรงพยาบาลไม่ถูกต้อง")
            ElseIf cboAnalyzer.SelectedValue = 0 Then
                MessageBox.Show("กรุณาเลือก Analyzer")
            Else
                If rbtStsYes.Checked = True Then
                    tmpActive = 1
                    StrSql = "UPDATE customer_analyzer_list SET hos_code='" & tbHosCode.Text & "',analyzer_id=" & Convert.ToInt32(cboAnalyzer.SelectedValue) & ",analyzer_name='" & cboAnalyzer.Text & "',install_date='" & dtpInstallDate.Value.ToString("yyyy-M-d", CultureInfo.CreateSpecificCulture("en-GB")) & "',install_staff='" & tbStaff.Text & "',active=" & tmpActive & ",po_no='" & tbPONo.Text & "',inv_no='" & tbInvNo.Text & "' WHERE customer_analyzer_list_id=" & pCALID & ";"
                Else
                    tmpActive = 0
                    StrSql = "UPDATE customer_analyzer_list SET hos_code='" & tbHosCode.Text & "',analyzer_id=" & Convert.ToInt32(cboAnalyzer.SelectedValue) & ",analyzer_name='" & cboAnalyzer.Text & "',install_date='" & dtpInstallDate.Value.ToString("yyyy-M-d", CultureInfo.CreateSpecificCulture("en-GB")) & "',cancel_date='" & dtpCancelDate.Value.ToString("yyyy-M-d", CultureInfo.CreateSpecificCulture("en-GB")) & "',install_staff='" & tbStaff.Text & "',active=" & tmpActive & ",po_no='" & tbPONo.Text & "',inv_no='" & tbInvNo.Text & "' WHERE customer_analyzer_list_id=" & pCALID & ";"
                End If

                Using StrWer As New StreamReader(Application.StartupPath & "\AppConn.ini")
                    Using objConn As New MySqlConnection(StrWer.ReadLine)
                        objConn.Open()
                        Using trans As MySqlTransaction = objConn.BeginTransaction
                            Using objCmd As New MySqlCommand(StrSql, objConn)
                                RowExc = objCmd.ExecuteNonQuery
                                If RowExc > 0 Then
                                    trans.Commit()
                                    'customer_analyzer_list.btSearchCAL.PerformClick()
                                    Call customer_analyzer_list.LoadCustAnalyzerList()
                                    MessageBox.Show("แก้ไขเรียบร้อย")
                                    'Call customer_analyzer_list.btSearchCAL_Click(sender, e)
                                    Me.Close()
                                Else
                                    trans.Rollback()
                                    MessageBox.Show("แก้ไขไม่สำเร็จ")
                                End If
                            End Using
                        End Using
                    End Using
                End Using
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString)
        End Try
    End Sub


Private Sub btClose_Click(sender As Object, e As EventArgs) Handles btClose.Click
        Try
            'Call customer_analyzer_list.LoadCustAnalyzerList()
            'customer_analyzer_list.btSearchCAL.PerformClick()
            Me.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString)
        End Try
    End Sub

Load data > open edit form > edit data : working fine but data at DataGridview in customer_analyzer_list  not change. LOL

I tried to change position of call load data line but not work.

*call load data and buttonsearch.performclick not working both.

 What am I missing or any example for using statement with try catch , Any suggestion?

Thank you.

解决方案

*call load data and buttonsearch.performclick not working both.

 What am I missing or any example for using statement with try catch , Any suggestion?

Is this the call that you are expecting to update the DGV?
    Call customer_analyzer_list.LoadCustAnalyzerList()

You haven't shown any code that sets the value of 'customer_analyzer_list' to the instance of the list form that contains the DGV that you want to update.    Typically, this value will be set from an argument that you pass to the constructor of the edit form.  If you are relying on the default value for this variable, that will be unreliable.  You should insert a breakpoint at the start of the method you are calling in the list form and make sure the code is reaching that point.  If it isn't then the likely problem is that you are referring to the wrong instance of the list form. 

If you prove that you are referring to the correct instance of the list form then you need to ensure that the DGV is refreshed after the data source is changed.  See:
https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.resetbindings%28v=vs.110%29.aspx



这篇关于从form2编辑后,form1中的DataGridview不会更新(使用语句,Try Catch语句)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆