How to insert new blank row in datagridview while it binding data source vb.net - TOP10

How to insert new blank row in datagridview while it binding data source vb.net


> Before Do This Please Select Data From server to fill In your DataTable And binding it to DataGridView.

Step 1 : Create VB.NET Form and choose DataGridview Control 

Step 2 : Choose ContextMenuStrip Control



Step 3: Create Menu Name (InsertNewRow)


Step 4 : Create Event on Insert New Row (Double Click on it)

Write code like below to create new DataRow in your DataTable that is Binding to DataGridview

Private Sub InsertNewRowToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles InsertNewRowToolStripMenuItem.Click

           Dim dr As DataRow = Me.YourDataTable.NewRow()
            Me.YourDataTable.Rows.InsertAt(dr, Me.YourDataGridview.CurrentCell.RowIndex)

End Sub

Step 5: Create Event on your DataGridview (Use Event MousUp)

Private Sub YourDataGridview_CellMouseUp(sender As Object, e As DataGridViewCellMouseEventArgs) Handles gridviewms.CellMouseUp

        Try
            If e.Button = MouseButtons.Right Then
                
                Me.gridviewms.Rows(e.RowIndex).Selected = True
               
                rowIndex = e.RowIndex

                Me.YourDataGridview.CurrentCell = Me.YourDataGridview.Rows(e.RowIndex).Cells(0)

                Me.ContextMenuStrip2.Show(Me.YourDataGridview, e.Location)

                Me.ContextMenuStrip2.Show(Cursor.Position)

            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub





Previous
Next Post »