내 데이터 그램에서 내 데이터베이스로 데이터를 삽입 할 때 데이터가없는 끝에 추가 행이 삽입됩니다. 이 문제를 어떻게 극복 할 수 있습니까? 내 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.
감사합니다 아래 예제 코드를 참조 루프 내부 검사를 추가'AllowUserToAddRows = FALSE '하지만 난 나를 떠나 전혀 행을 추가 할 수 있다고했을 때 빈 gridview. – user765942
자, DBNull 검사가 필요합니다. – Simon