Copy Paste Databound Row in Same Datagridview

Hobbyist_programmer 621 Reputation points
2022-07-05T19:29:48.28+00:00

Hallo,

I have an access database bound to a data grid view using a binding source. I want to make a copy of a current row in the data grid view or binding source with a button click. I tried following but it gives me a row that already belongs to the data table

        Dim rowView As DataRowView = DirectCast(BS.Current, DataRowView)     
  
        Dim nRow As DataRowView = DirectCast(BS.AddNew(), DataRowView)  
        nRow.BeginEdit()  
        nRow.Row.BeginEdit()  
        nRow.DataView.Table.Rows.Add(rowView)  
        BS.MoveLast()  

Datatable has a auto field primary column

Thanks

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,779 questions
0 comments No comments
{count} votes

Accepted answer
  1. LesHay 7,136 Reputation points
    2022-07-05T20:23:39.433+00:00

    Hi
    Give this a try and see if it helps.

    		Dim rowView() As Object = DirectCast(BS.Current, DataRowView).Row.ItemArray  
    		Dim nRow As DataRow = dt.NewRow  
    		nRow.ItemArray = rowView  
      
    		' here I have used InsertAt but  
    		' just .Add would work too  
    		dt.Rows.InsertAt(nRow, BS.Position)  
    

1 additional answer

Sort by: Most helpful
  1. LesHay 7,136 Reputation points
    2022-07-08T12:51:33.797+00:00

    Hi

    Try this variation (whole test project posted but see changes in last sub)

    ' Form1 with DataGridView1,  
    ' RadioButtons 1 and 2  
    ' Label1 and Button1  
    Option Strict On  
    Option Explicit On  
    Public Class Form1  
    	Dim dt As New DataTable("Freddy")  
    	Dim BS As New BindingSource  
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
    		With dt  
    			.Columns.Add("ID", GetType(Integer))  
      
    			.PrimaryKey = New DataColumn() { .Columns("ID")}  
    			With .Columns("ID")  
    				.AutoIncrement = True  
    				.AutoIncrementSeed = 1  
    				.AutoIncrementStep = 1  
    			End With  
      
    			.Columns.Add("Catagory", GetType(Double))  
    			.Columns.Add("Product", GetType(Double))  
    		End With  
      
    		' few sample data values  
    		dt.Rows.Add(Nothing, 12.34, 21.32)  
    		dt.Rows.Add(Nothing, 1.3, 1.2)  
    		dt.Rows.Add(Nothing, 2.4, 2.3)  
      
    		BS.DataSource = dt  
    		DataGridView1.DataSource = BS  
    	End Sub  
    	Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged  
    		Dim rb As RadioButton = DirectCast(sender, RadioButton)  
    		If rb.Checked Then  
    			Label1.Text = CDbl(dt.Compute("SUM(Product)", String.Empty)).ToString("C2")  
    		Else  
    			Label1.Text = 0.ToString("C2")  
    		End If  
    	End Sub  
      
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
    		Dim rowView() As Object = DirectCast(BS.Current, DataRowView).Row.ItemArray  
      
    		' contrained column  
    		rowView(0) = Nothing  
      
    		Dim nRow As DataRow = dt.NewRow  
    		nRow.ItemArray = rowView  
      
    		' .Add row to bottom  
    		dt.Rows.Add(nRow)  
    	End Sub  
    End Class  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.