2011-11-09 3 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

감사합니다 아래 예제 코드를 참조 루프 내부 검사를 추가'AllowUserToAddRows = FALSE '하지만 난 나를 떠나 전혀 행을 추가 할 수 있다고했을 때 빈 gridview. – user765942

+1

자, DBNull 검사가 필요합니다. – Simon

0

이미 해봤

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 
관련 문제