正确使用和过渡子 [英] Proper use and Transition of Subs

查看:213
本文介绍了正确使用和过渡子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的图像。

I have an Image here that looks like this.

这个代码一起在两个Subs中,即

This code together is inside two Subs namely

Public Sub Import_Data Public Sub Insert_Data

为了更清楚我将把代码放在Transition中,这里是。

To make it clearer I will put the codes in Transition and here it is.

我将点击按钮导入

      Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    Import_Data
    Insert_Data
    End Sub

    Sub import_data
    'This is the code when I put the excel data in datagridview
      Dim conn As OleDbConnection
                Dim dta As OleDbDataAdapter
                Dim dts As DataSet
                Dim excel As String
                Dim OpenFileDialog As New OpenFileDialog
                OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
                OpenFileDialog.Filter = "All Files (*.*)|*.*|Excel files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv|XLS Files (*.xls)|*xls"
                If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
                    Dim fi As New FileInfo(OpenFileDialog.FileName)
                    Dim FileName As String = OpenFileDialog.FileName
                    excel = fi.FullName
                    conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excel + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1';")
                    dta = New OleDbDataAdapter("Select F3 as ItemCode,F4 as Description,F6 as OrderedQty,F9 as Remarks,F10 as Tag From [Official Transmittal Form$]", conn)
                    dts = New DataSet
                    dta.Fill(dts, 13, 768, "MyTable")
                    DataGridView1.DataSource = dts
                    DataGridView1.DataMember = "MyTable"
                    conn.Close()
    End if
    End Sub

Sub Insert_Data
'This is the code that will save all data in the datagridview into MYSQL
    Try
            Dim myconnect As MySqlConnection = New MySqlConnection("datasource=192.168.2.254;database=inventory;userid=root;password=admin1950")
            Dim queryInsert As String = "insert into stockrequisition (SRNumber,ItemCode,Description,RequestedQty,Remarks) values (@SRNumber,@ItemCode,@Description,@RequestedQty,@Remarks)"
            Dim cmd = New MySqlCommand(queryInsert, myconnect)
            cmd.Parameters.Add("@SRNumber", MySqlDbType.VarChar)
            cmd.Parameters.Add("@ItemCode", MySqlDbType.VarChar)
            cmd.Parameters.Add("@Description", MySqlDbType.VarChar)
            cmd.Parameters.Add("@RequestedQty", MySqlDbType.VarChar)
            cmd.Parameters.Add("@Remarks", MySqlDbType.VarChar)

            For x As Integer = 0 To DataGridView1.Rows.Count - 1
                cmd.Parameters("@SRNumber").Value = TextBox1.Text
                cmd.Parameters("@ItemCode").Value = (CStr(DataGridView1.Rows(x).Cells("ItemCode").Value.ToString))
                cmd.Parameters("@Description").Value = (CStr(DataGridView1.Rows(x).Cells("Description").Value.ToString))
                Try
                    cmd.Parameters("@RequestedQty").Value = (FormatNumber(CStr(DataGridView1.Rows(x).Cells("OrderedQty").Value.ToString), TriState.True, TriState.True))

                Catch
                    cmd.Parameters("@RequestedQty").Value = "0.00"
                End Try
                cmd.Parameters("@Remarks").Value = (CStr(DataGridView1.Rows(x).Cells("Remarks").Value.ToString))
                myconnect.Open()
                cmd.ExecuteNonQuery()
                myconnect.Close()

            Next

        Catch

        End Try

    End Sub

现在删除具有排除数据我把这个代码放在 DGV1_Cellformating

Now to remove the rows that has a Exclude data I put this code inside the DGV1_Cellformating

Private Sub DGV1_Cellformating
    Dim resultSet = From drow As DataGridViewRow In DataGridView1.Rows _
    Where drow.Cells(4).Value.ToString.ToUpper = "EXCLUDE" _
    Select drow.Index
    For Each rowIndex In resultSet
    DataGridView1.Rows.RemoveAt(rowIndex)
    Next
    End Sub

现在这里是我的最终目标/问题
如何使用排除数据之前删除所有行,然后将其保存到数据库?

Now here is my final Goal/Question How can make remove all rows with Exclude data before saving it to database?

例如在上面的第一张图片上,所有包含 Include 数据的行将是唯一的数据将被保存在数据库中。

For example on the first image above that all row with Include data will be the only datas will be saved in database.

我的代码中发生了什么,所有数据都已保存在数据库中, Include Exclude

What happens in my code is all the data has been saved in database the Include and Exclude

TYSM将来的帮助

推荐答案

可能您正在删除错误索引中的行。

it's probable that you are removing rows at the wrong index.

尝试以下方式:

For R As Integer = DataGridView1.RowCount - 1 To 0 Step -1
    If DataGridView1.Item(4, R).Value.ToString = "Exclude" Then
        DataGridView1.Rows.RemoveAt(R)
    End If
Next

这篇关于正确使用和过渡子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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