I am new to vb.net, I want the first column of the unbound datagridview to have defult value such as item1,item2. item3 on form load event . I would appriciate if someone tell me how to add defult values in a column like this and save all three rows in the table.
Dim thisConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dtb.accdb")
'Create Command object
Dim nonqueryCommand As OleDbCommand = thisConnection.CreateCommand()
Try
' Open Connection
thisConnection.Open()
Console.WriteLine("Connection Opened")
' Create INSERT statement with named parameters
nonqueryCommand.CommandText = _
"INSERT INTO myTable (Col1, Col2) VALUES (@Col1, @Col2)"
' Add Parameters to Command Parameters collection
nonqueryCommand.Parameters.Add("@Col1", OleDbType.VarChar, 50)
nonqueryCommand.Parameters.Add("@Col2", OleDbType.VarChar, 50)
' Prepare command for repeated execution
nonqueryCommand.Prepare()
' Data to be inserted
For Each row As DataGridViewRow In DataGridView1.Rows
If Not row.IsNewRow Then
nonqueryCommand.Parameters("@Col1").Value = row.Cells(0).Value.ToString
nonqueryCommand.Parameters("@Col2").Value = row.Cells(1).Value.ToString
End If
Next
nonqueryCommand.ExecuteNonQuery()
Catch ex As OleDbException
' Display error
Console.WriteLine("Error: " & ex.ToString())
Finally
' Close Connection
thisConnection.Close()
Console.WriteLine("Connection Closed")
End Try