none
vb2008 Datagridview判斷問題 RRS feed

  • 問題

  • 親愛的各位大大:

          我有一個Datagridview的判斷問題想請教。問題是這樣的我有一個Datagridview 有很多欄位 我要判斷其中某一欄位是否有填,程式碼如下 是可以判斷的 但是如果某一欄位沒填它會秀出訊息

    問題是我要如何讓它(指cells(22) and Cells(24))完全填完才去執行SqlBulkCopy?

    For j = 0 To DataGridView2.Rows.Count - 1
         If DataGridView2.Rows(j).Cells(22).Value.ToString = "" Or DataGridView2.Rows(j).Cells

          (24).Value = 0 Then
          MessageBox.Show("供應商或詢價數量未填,且詢價數量不得為零,請補齊相關資料")

           endif

    next

                    Dim strDbCon1 As String
                    Dim ds1 As New DataSet
                    Dim cmd As New SqlCommand
                    Dim MyBulkcopy As SqlBulkCopy
                    Dim TA001, TB002, TB003, TA012, TA003, TB004, TB005, TB006, MB064, TB009, MB036, MB039, TB007, TB011, TB032, MA002, TB016, TB017, MB067, TC001, TC002, TD003, TD017, TY016, TD008, TD011, TC006, TC007, TD007, TD012, TC011 As SqlBulkCopyColumnMapping
                    Dim myDatatable3 As New DataTable
                    myDatatable3 = DataGridView2.DataSource
                    strDbCon1 = "Persist Security Info=False;User ID=sa;pwd=sa;Initial Catalog=purchase;Data Source=192.195.68.50"
                    conn1 = New SqlConnection(strDbCon1)
                    conn1.Open()
                    MyBulkcopy = New SqlBulkCopy(strDbCon1)
                    MyBulkcopy.DestinationTableName = "PURS"
                    TA001 = New SqlBulkCopyColumnMapping("TA001", "TA001")
                    TB002 = New SqlBulkCopyColumnMapping("TB002", "TB002")
                    TB003 = New SqlBulkCopyColumnMapping("TB003", "TB003")
                    TA012 = New SqlBulkCopyColumnMapping("TA012", "TA012")
                    TA003 = New SqlBulkCopyColumnMapping("TA003", "TA003")
                    TB004 = New SqlBulkCopyColumnMapping("TB004", "TB004")
                    TB005 = New SqlBulkCopyColumnMapping("TB005", "TB005")
                    TB006 = New SqlBulkCopyColumnMapping("TB006", "TB006")
                    MB064 = New SqlBulkCopyColumnMapping("MB064", "MB064")
                    TB009 = New SqlBulkCopyColumnMapping("TB009", "TB009")
                    MB036 = New SqlBulkCopyColumnMapping("MB036", "MB036")
                    MB039 = New SqlBulkCopyColumnMapping("MB039", "MB039")
                    TB007 = New SqlBulkCopyColumnMapping("TB007", "TB007")
                    TB011 = New SqlBulkCopyColumnMapping("TB011", "TB011")
                    TB032 = New SqlBulkCopyColumnMapping("TB032", "TB032")
                    MA002 = New SqlBulkCopyColumnMapping("MA002", "MA002")
                    TB016 = New SqlBulkCopyColumnMapping("TB016", "TB016")
                    TB017 = New SqlBulkCopyColumnMapping("TB017", "TB017")
                    MB067 = New SqlBulkCopyColumnMapping("MB067", "MB067")
                    TC001 = New SqlBulkCopyColumnMapping("採購單別", "TC001")
                    TC002 = New SqlBulkCopyColumnMapping("採購單號", "TC002")
                    TD003 = New SqlBulkCopyColumnMapping("採購序號", "TD003")
                    TD017 = New SqlBulkCopyColumnMapping("供應商", "TD017")
                    TY016 = New SqlBulkCopyColumnMapping("幣別", "TY016")
                    TD008 = New SqlBulkCopyColumnMapping("詢價數量", "TD008")
                    TD011 = New SqlBulkCopyColumnMapping("單價", "TD011")
                    TC006 = New SqlBulkCopyColumnMapping("SCM序號", "TC006")
                    TC007 = New SqlBulkCopyColumnMapping("指定廠商", "TC007")
                    TD007 = New SqlBulkCopyColumnMapping("交貨地", "TD007")
                    TD012 = New SqlBulkCopyColumnMapping("詢價日期", "TD012")
                    TC011 = New SqlBulkCopyColumnMapping("詢價人員", "TC011")


                    MyBulkcopy.ColumnMappings.Add(TA001)
                    MyBulkcopy.ColumnMappings.Add(TB002)
                    MyBulkcopy.ColumnMappings.Add(TB003)
                    MyBulkcopy.ColumnMappings.Add(TA012)
                    MyBulkcopy.ColumnMappings.Add(TA003)
                    MyBulkcopy.ColumnMappings.Add(TB004)
                    MyBulkcopy.ColumnMappings.Add(TB005)
                    MyBulkcopy.ColumnMappings.Add(TB006)
                    MyBulkcopy.ColumnMappings.Add(MB064)
                    MyBulkcopy.ColumnMappings.Add(TB009)
                    MyBulkcopy.ColumnMappings.Add(MB036)
                    MyBulkcopy.ColumnMappings.Add(MB039)
                    MyBulkcopy.ColumnMappings.Add(TB007)
                    MyBulkcopy.ColumnMappings.Add(TB011)
                    MyBulkcopy.ColumnMappings.Add(TB032)
                    MyBulkcopy.ColumnMappings.Add(MA002)
                    MyBulkcopy.ColumnMappings.Add(TB016)
                    MyBulkcopy.ColumnMappings.Add(TB017)
                    MyBulkcopy.ColumnMappings.Add(MB067)
                    MyBulkcopy.ColumnMappings.Add(TC001)
                    MyBulkcopy.ColumnMappings.Add(TC002)
                    MyBulkcopy.ColumnMappings.Add(TD003)
                    MyBulkcopy.ColumnMappings.Add(TD017)
                    MyBulkcopy.ColumnMappings.Add(TY016)
                    MyBulkcopy.ColumnMappings.Add(TD008)
                    MyBulkcopy.ColumnMappings.Add(TD011)
                    MyBulkcopy.ColumnMappings.Add(TC006)
                    MyBulkcopy.ColumnMappings.Add(TC007)
                    MyBulkcopy.ColumnMappings.Add(TD007)
                    MyBulkcopy.ColumnMappings.Add(TD012)
                    MyBulkcopy.ColumnMappings.Add(TC011)
                    MyBulkcopy.WriteToServer(myDatatable3)
                    MessageBox.Show("已存檔")
          

    感謝各位大大

     

    2010年8月4日 上午 01:58

解答

  • Hello, 以下供您參考, 若有誤會題意請見諒.

    如果 SqlBulkCopy 程序之後沒有任何程序了, 當檢查不應執行時就直接 Exit Function 或 Sub:
    -----
    For ~~~
       If ~~~ Then
          MessageBox.Show(~~~)

          Exit Function 或 Sub
       End If
    Next

    SqlBulkCopy 程序 ~~~
    -----

    如果 SqlBulkCopy 程序之後還有其他程序, 個人習慣是用個旗標變數判斷:
    -----
    Dim FlagOK As Boolean = True
    For ~~~
       If ~~~ Then
          MessageBox.Show(~~~)

          FlagOK = False
          Exit For
        
    End If
    Next

    If FlagOK Then
       SqlBulkCopy 程序 ~~~
    End If
    -----

    • 已編輯 Ader.Chen 2010年8月4日 下午 03:56 更正:刪除Exit For
    • 已標示為解答 MichaelTsai10 2010年8月5日 上午 02:44
    2010年8月4日 上午 05:15

所有回覆

  • 親愛的各位大大:

    後來我加了Goto 但是還是有個問題程式碼如下,當跳到220:後,修改正確(表示把缺少資料填入後)在存檔過程中MessageBox.Show("供應商或詢價數量未填,且詢價數量不得為零,請補齊相關資料")這個messagebox.show還會跑出一次如何避免它再出現一次呢?感謝各位大大

     For j = 0 To DataGridView2.Rows.Count - 1
                If DataGridView2.Rows(j).Cells(22).Value.ToString = "" Or DataGridView2.Rows(j).Cells(24).Value = 0 Then
                    GoTo 220
                    Exit For
                End If
            Next
            myDatatable3 = DataGridView2.DataSource
            MyBulkcopy = New SqlBulkCopy(strDbCon1)
            MyBulkcopy.DestinationTableName = "PURS"
            TA001 = New SqlBulkCopyColumnMapping("TA001", "TA001")
            TB002 = New SqlBulkCopyColumnMapping("TB002", "TB002")
            TB003 = New SqlBulkCopyColumnMapping("TB003", "TB003")
            TA012 = New SqlBulkCopyColumnMapping("TA012", "TA012")
            TA003 = New SqlBulkCopyColumnMapping("TA003", "TA003")
            TB004 = New SqlBulkCopyColumnMapping("TB004", "TB004")
            TB005 = New SqlBulkCopyColumnMapping("TB005", "TB005")
            TB006 = New SqlBulkCopyColumnMapping("TB006", "TB006")
            TB009 = New SqlBulkCopyColumnMapping("TB009", "TB009")
            TB007 = New SqlBulkCopyColumnMapping("TB007", "TB007")
            TB011 = New SqlBulkCopyColumnMapping("TB011", "TB011")
            TB032 = New SqlBulkCopyColumnMapping("TB032", "TB032")
            TB016 = New SqlBulkCopyColumnMapping("TB016", "TB016")
            TB017 = New SqlBulkCopyColumnMapping("TB017", "TB017")
            TC001 = New SqlBulkCopyColumnMapping("採購單別", "TC001")
            TC002 = New SqlBulkCopyColumnMapping("採購單號", "TC002")
            TD003 = New SqlBulkCopyColumnMapping("採購序號", "TD003")
            TD017 = New SqlBulkCopyColumnMapping("供應商", "TD017")
            TY016 = New SqlBulkCopyColumnMapping("幣別", "TY016")
            TD008 = New SqlBulkCopyColumnMapping("詢價數量", "TD008")
            TD011 = New SqlBulkCopyColumnMapping("單價", "TD011")
            TC006 = New SqlBulkCopyColumnMapping("SCM序號", "TC006")
            TC007 = New SqlBulkCopyColumnMapping("指定廠商", "TC007")
            TD007 = New SqlBulkCopyColumnMapping("交貨地", "TD007")
            TD012 = New SqlBulkCopyColumnMapping("詢價日期", "TD012")
            TC011 = New SqlBulkCopyColumnMapping("詢價人員", "TC011")


            MyBulkcopy.ColumnMappings.Add(TA001)
            MyBulkcopy.ColumnMappings.Add(TB002)
            MyBulkcopy.ColumnMappings.Add(TB003)
            MyBulkcopy.ColumnMappings.Add(TA012)
            MyBulkcopy.ColumnMappings.Add(TA003)
            MyBulkcopy.ColumnMappings.Add(TB004)
            MyBulkcopy.ColumnMappings.Add(TB005)
            MyBulkcopy.ColumnMappings.Add(TB006)
            MyBulkcopy.ColumnMappings.Add(TB009)
            MyBulkcopy.ColumnMappings.Add(TB007)
            MyBulkcopy.ColumnMappings.Add(TB011)
            MyBulkcopy.ColumnMappings.Add(TB032)
            MyBulkcopy.ColumnMappings.Add(TB017)
            MyBulkcopy.ColumnMappings.Add(TC001)
            MyBulkcopy.ColumnMappings.Add(TC002)
            MyBulkcopy.ColumnMappings.Add(TD003)
            MyBulkcopy.ColumnMappings.Add(TD017)
            MyBulkcopy.ColumnMappings.Add(TY016)
            MyBulkcopy.ColumnMappings.Add(TD008)
            MyBulkcopy.ColumnMappings.Add(TD011)
            MyBulkcopy.ColumnMappings.Add(TC006)
            MyBulkcopy.ColumnMappings.Add(TC007)
            MyBulkcopy.ColumnMappings.Add(TD007)
            MyBulkcopy.ColumnMappings.Add(TD012)
            MyBulkcopy.ColumnMappings.Add(TC011)
            MyBulkcopy.WriteToServer(myDatatable3)
            MessageBox.Show("已存檔")
    220:
            MessageBox.Show("供應商或詢價數量未填,且詢價數量不得為零,請補齊相關資料")

    2010年8月4日 上午 03:00
  • 建議可以將程式改成這樣:

    For j = 0 To DataGridView2.Rows.Count - 1
         If DataGridView2.Rows(j).Cells(22).Value.ToString = "" Or DataGridView2.Rows(j).Cells

          (24).Value = 0 Then
          MessageBox.Show("供應商或詢價數量未填,且詢價數量不得為零,請補齊相關資料")

          Exit For

          Else

             '執行SqlBulkCopy

           endif

    next

     

     

    2010年8月4日 上午 03:52
  • Hello, 以下供您參考, 若有誤會題意請見諒.

    如果 SqlBulkCopy 程序之後沒有任何程序了, 當檢查不應執行時就直接 Exit Function 或 Sub:
    -----
    For ~~~
       If ~~~ Then
          MessageBox.Show(~~~)

          Exit Function 或 Sub
       End If
    Next

    SqlBulkCopy 程序 ~~~
    -----

    如果 SqlBulkCopy 程序之後還有其他程序, 個人習慣是用個旗標變數判斷:
    -----
    Dim FlagOK As Boolean = True
    For ~~~
       If ~~~ Then
          MessageBox.Show(~~~)

          FlagOK = False
          Exit For
        
    End If
    Next

    If FlagOK Then
       SqlBulkCopy 程序 ~~~
    End If
    -----

    • 已編輯 Ader.Chen 2010年8月4日 下午 03:56 更正:刪除Exit For
    • 已標示為解答 MichaelTsai10 2010年8月5日 上午 02:44
    2010年8月4日 上午 05:15
  • 親愛的大大:

          感謝您的支持與回覆。但它有個問題就是當第一列合乎判斷式時而2到n列不合時它還是會存入第一列。當使用者重新補齊資料重新按存檔剛才的那一列又被存入造成重覆,除非有相關配套,也就是在存入過程中比對資料是否存在 ,如果存在用更新 不存在用insert。我私下以為這樣才ok,但問題是我這樣的程式碼不知該怎麼寫?如果大大您會寫的話也請您告知。最後感謝您的支持與回覆。

    2010年8月5日 上午 02:58
  • 1. ??
    -----
    Dim FlagOK As Boolean = True
    For j = 0 To DataGridView2.Rows.Count - 1
       If (DataGridView2.Rows(j).Cells(22).Value.ToString = "" ) OrElse _
          (DataGridView2.Rows(j).Cells(24).Value = 0) Then

          MessageBox.Show(~~~)

          FlagOK = False
          Exit For
       End If
    Next

    If FlagOK Then
       SqlBulkCopy 程序 ~~~
    End If
    -----

    SqlBulkCopy 程序是放在迴圈外, 應該是全部檢查都通過(沒有進入If ~Then 裡面)才會執行, 不應該會偷跑啊?
    請用中斷點再逐步偵錯看流程. 重複資料有無可能是之前新增的, 這次重複輸入?

    2. 您之前的提問: http://social.msdn.microsoft.com/Forums/zh-TW/232/thread/a7d9d12d-c645-4e3a-a44c-2de2336aed55/
        心冷熱情熄 前輩的回答,

    (我還是屬於要不斷查網路資料寫程式的初級者)

     

    2010年8月5日 上午 04:52
  • 親愛的大大:

     

           您的這組程式是ok的    感謝您

     

    祝您假期愉快

    2010年8月6日 上午 02:19