2011-11-09 21 views
0

私のデータベースに私のdatagridviewからデータを挿入すると、余分な行がデータを含まない最後に挿入されます。どうすればこの問題を克服できますか?データグリッドビューで新しい行をクリックして入力データを開始するたびに、新しい行が下に有効になります。この空白行をデータベースに挿入するのを止める方法はありますか?おかげで、ここにあなたがそれを保存する前にDBNull.Valueのための各セル(または特定のセル)をチェックしてみてください、あなたのループであなたのチェック値のためのあなたの行が必要になります...データベースに余分な行を挿入する挿入

    Dim ds As New DataSet 
        ds.Tables.Add("Main") 

        Dim col As New DataColumn 

        Dim dgvCol As New DataGridViewColumn 
        For Each dgvCol In DataGridView1.Columns 
         col = New DataColumn(dgvCol.Name) 
         ds.Tables("Main").Columns.Add(col) 
        Next 
        Dim row As DataRow 
        Dim colcount As Integer = DataGridView1.Columns.Count - 1 

        For i As Integer = 0 To DataGridView1.Rows.Count - 1 
         row = ds.Tables("Main").Rows.Add 

         For Each column As DataGridViewColumn In DataGridView1.Columns 
          row.Item(column.Index) = DataGridView1.Rows.Item(i).Cells(column.Index).Value 
         Next 

        Next 


        Dim con As New SqlClient.SqlConnection 
        Dim myCommand As New SqlClient.SqlCommand 

        Dim dt As New DataTable() 
        dt = ds.Tables("Main") 

        For i As Integer = 0 To dt.Rows.Count - 1 

         Dim myAdapter As New SqlClient.SqlDataAdapter 
         Dim sql As String = "Insert into details (company, division, date, supplier, material_group, cost, dsc, email, userID, fullName, marketingCode, hccNumber, wbs, qty, currency, timestamp) VALUES ('" & DataGridView1.Rows(i).Cells(company.Name).Value & "', '" & DataGridView1.Rows(i).Cells(division.Name).Value & "','" & calendar & "', '" & DataGridView1.Rows(i).Cells(supplier.Name).Value & "', '" & DataGridView1.Rows(i).Cells(materialGroup.Name).Value & "', '" & DataGridView1.Rows(i).Cells(netprice.Name).Value & "', '" & DataGridView1.Rows(i).Cells(description.Name).Value & "', '" & cmbEmail.SelectedValue & "', '" & id & "', '" & newName & "', '" & DataGridView1.Rows(0).Cells(markCodes.Name).Value & "', '" & DataGridView1.Rows(0).Cells(hccNumber.Name).Value & "', '" & DataGridView1.Rows(i).Cells(wba.Name).Value & "', '" & DataGridView1.Rows(i).Cells(quantity.Name).Value & "', '" & DataGridView1.Rows(i).Cells(currency.Name).Value & "', '" & nowDate & "')" 
         myCommand.Connection = con 
         myCommand.CommandText = sql 
         myAdapter.InsertCommand = myCommand 
         myCommand.Connection.Open() 
         myCommand.ExecuteNonQuery() 
         myCommand.Connection.Close() 

        Next. 

答えて

1

を私のコードです。空白の行が表示されないようにするには、AllowUsersToAddRowsをfalseに設定することもできます。

+0

以下のサンプルコードを参照してください。ループ内でチェックを追加します。空のグリッドビュー。 – user765942

+1

さて、DBNullチェックが必要です。 – Simon

0

はおかげで、私はすでに `AllowUserToAddRows = false`を試したが、私は私が私を残し、まったくの行を追加することができなかったことをやったとき

For Each rw As DataGridViewRow In Form1.DataGridView1.Rows 

      If rw.Cells("Column2").Value <> Nothing Then 

       ITM_ID = rw.Cells("Column1").Value 
       DSPL_RCT = rw.Cells("Column2").Value 
       SLS_QTY = rw.Cells("Column3").Value 
       SLS_PRC = rw.Cells("Column4").Value 
       SLS_AMT = rw.Cells("Column5").Value 
       TAX_CODE = rw.Cells("Column6").Value 

       'INSERT A RECORD INTO THE DATABASE 

       Dim cmd As New SqlCommand("INSERT INTO DAY_PLUSALES (DT,ITM_ID,DSPL_RCT,SLS_QTY,SLS_PRC,SLS_AMT,TAX_CODE) values ('" & Form1.TextBox6.Text & "','" & ITM_ID & "','" & DSPL_RCT & "','" & SLS_QTY & "','" & SLS_PRC & "','" & SLS_AMT & "','" & TAX_CODE & "')", con) 

       cmd.ExecuteNonQuery() 

End If 

     Next 
関連する問題