none
想请教有关detect datagridview Colum 相同字的问题 RRS feed

  • 问题

  •  我篇写了upload 文件的代码。

    我遇到的问题是,有时当user upload相同的文件名,文件不会被overwrite 反之还会upload成功如:下图。

    以下是我的代码清查看。谢谢

    Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
            Dim DateNow0 = Date.Now.ToString("yyyyMMdd")
            Dim ProductID As String
            Dim File_Location As String

            ProductID = txtProductID.Text
            File_Location = txtFile_Location.Text

            OpenFileDialog1.Title = "Open File"
            OpenFileDialog1.Filter = "All files (*.*)|*.*"
            OpenFileDialog1.InitialDirectory = "D:\"
            OpenFileDialog1.Multiselect = False
            OpenFileDialog1.FileName = String.Empty
            '' If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then


            If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                Dim sign As Boolean = True
                For Each itm As DataGridViewRow In DataGridView1.Rows
                    Dim onlyfilename As String = Path.GetFileName(OpenFileDialog1.FileName)
                    txtFile_Name.Text = onlyfilename
                    If onlyfilename = DataGridView1.CurrentRow.Cells(2).Value.ToString Then
                        Select Case MsgBox("A file with the same name already exists in the database. Are you sure to overwrite it?", MsgBoxStyle.YesNo, "Message Confirmation")

                            Case MsgBoxResult.Yes
                                Dim connection As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + strFullDBPath + ";Jet OLEDB:Database Password=123456;")

                                Dim cmd As New OleDbCommand("delete * from Table1 where filename='" + onlyfilename + "'", connection)

                                connection.Open()

                                cmd.ExecuteNonQuery()

                                connection.Close()

                                filePath = OpenFileDialog1.FileName

                                fileName = OpenFileDialog1.SafeFileName

                                UploadFile(filePath, ProductID, File_Location, fileName)

                                RefreshData()

                                MessageBox.Show("Your data have been overwrite", "Message")
                                Exit Sub
                                MessageBox.Show("Your data have been keep", "Message")
                                sign = False
                                Exit Sub
                        End Select
                    End If
                Next

                If sign Then
                    filePath = OpenFileDialog1.FileName
                    fileName = OpenFileDialog1.SafeFileName
                    UploadFile(filePath, ProductID, File_Location, fileName)
                    RefreshData()
                End If

            End If

        End Sub

           
    2020年10月28日 0:55

答案

  • Hi christing,

    经过我的测试发现,你通过 ‘DataGridView1.CurrentRow’ 来获得 ‘FileName’ 列中对应的值,但是currentRow只是获得的是鼠标选择的列,你需要利用遍历中的 'itm'值获取每个列的值进行比较。

    参考以下代码:

        If DataGridView1.Rows.Count > 0 Then
                    For Each itm As DataGridViewRow In DataGridView1.Rows
                        Dim onlyfilename As String = Path.GetFileName(OpenFileDialog1.FileName)
                        txtFile_Name.Text = onlyfilename
    
                        If onlyfilename = itm.Cells("FileName").Value.ToString Then
         '...

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 christing 2020年10月30日 2:21
    2020年10月29日 6:11
    版主

全部回复

  • Hi christing,

    我尝试使用你的代码进行测试,但缺少 'UploadFile' 和 'RefreshData' 方法部分的代码,并且尝试使用你之前帖子中的代码时也出现了一些错误。

    能否提供完整的代码(包括设置 DataGridView 的部分)?这将有助于我们更好的分析以及重现你的问题,并进行相应的测试。

    期待你的更新。

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年10月28日 8:28
    版主
  • @XING YU ZHAO

    感谢你的回复。

    以下是我的代码

       Sub RefreshData()
            Dim connection As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + strFullDBPath + ";Jet OLEDB:Database Password=123456;")
            Dim sda As New OleDbDataAdapter("select * from Table1", connection)
            Dim dt As New DataTable
            sda.Fill(dt)
            DataGridView1.DataSource = dt
            DataGridView1.Columns("Column1").DataPropertyName = "Product_ID"
            DataGridView1.Columns("Column2").DataPropertyName = "File_Location"
            DataGridView1.Columns("Column3").DataPropertyName = "FileName"

            Me.DataGridView1.Columns("Column2").Visible = False
        End Sub


        Sub UploadFile(ByVal filePath As String, ByVal ProductID As String, ByVal File_Location As String, ByVal fileName As String)

            Dim DateNow0 = Date.Now.ToString("yyyyMMdd")
            Dim buffer() As Byte
            Dim result As Integer
            If String.IsNullOrEmpty(filePath) Then
            Else
                buffer = File.ReadAllBytes(filePath)
                If buffer.Length > 100 * 2048 Then
                    MsgBox("文件过大")
                Else
                    Control_File()

                    Dim folderP As String = strCurrentPath + "\Document" + "\" + DateNow0
                    'Create File path
                    If Not Directory.Exists(folderP) Then
                        Directory.CreateDirectory(folderP)
                    End If

                    Dim Upload_Path As String = IO.Path.Combine(strCurrentPath + "\Document" + "\" + DateNow0)

                    '' If IO.Directory.Exists(Upload_Path) Then

                    'Try
                    IO.File.Copy(OpenFileDialog1.FileName, IO.Path.Combine(Upload_Path, IO.Path.GetFileName(OpenFileDialog1.FileName)), True)

                    ''IO.File.Copy(Path.Combine(filePath, OpenFileDialog1.FileName), Path.Combine(filePath, OpenFileDialog1.FileName), True)
                    'Catch ex As Exception
                    'End Try

                    Dim connection As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + strFullDBPath + ";Jet OLEDB:Database Password=123456;")
                    '' Dim cmd As New OleDbCommand("insert into Table1 (Product_ID,File_Location,filename,file) values(@Product_ID,@File_Location,@filename,@file)", connection)
                    Dim cmd As New OleDbCommand("insert into Table1 values(@Product_ID,@File_Location,@filename,@file)", connection)

                    cmd.Parameters.AddWithValue("@PRODUCT_ID", Me.txtProductID.Text)
                    cmd.Parameters.AddWithValue("@File_Location", Me.txtFile_Location.Text)
                    cmd.Parameters.AddWithValue("@filename", fileName)
                    cmd.Parameters.AddRange({New OleDbParameter("@file", buffer)})
                    connection.Open()
                    result = cmd.ExecuteNonQuery()
                    connection.Close()
                    MsgBox("Upload Success")
                End If
                End If

        End Sub

    2020年10月28日 9:33
  • Hi christing,

    经过我的测试发现,你通过 ‘DataGridView1.CurrentRow’ 来获得 ‘FileName’ 列中对应的值,但是currentRow只是获得的是鼠标选择的列,你需要利用遍历中的 'itm'值获取每个列的值进行比较。

    参考以下代码:

        If DataGridView1.Rows.Count > 0 Then
                    For Each itm As DataGridViewRow In DataGridView1.Rows
                        Dim onlyfilename As String = Path.GetFileName(OpenFileDialog1.FileName)
                        txtFile_Name.Text = onlyfilename
    
                        If onlyfilename = itm.Cells("FileName").Value.ToString Then
         '...

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 christing 2020年10月30日 2:21
    2020年10月29日 6:11
    版主
  • @Xingyu Zhao

    感谢你的回复。

    想请教你一个问题。怎样可以做到当user register 一个account 后,会有属于自己的profile它只看的到自己的东西别人看不到除了admin。

    2020年10月30日 2:21
  • Hi christing,

    >>会有属于自己的profile

    你能说明一下 ‘profile’ 具体指的是什么吗?

    另外,由于该问题和原始问题联系不大,我建议你创建一个新帖进行询问,我们会在新帖下为你提供建议。

    Best Regards,

    Xingyu Zhao

     

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年10月30日 2:29
    版主
  • @xingyuzhao

    我会在开新post

    2020年10月30日 3:54