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

Hi, maybe this helps you

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim temp As String = "item"
        DataGridView1.Rows.Add(3) ' number of rows you want
        DataGridView1.Rows(0).Cells("Column1").Value = temp
        DataGridView1.Rows(1).Cells("Column1").Value = temp
        DataGridView1.Rows(2).Cells("Column1").Value = temp
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.