How do I clone a row from a bound datagridview to another bound datagridview?

How do I clone a row from a bound datagridview to another bound datagridview?

I'm getting error that you cannot add a row to a bound datagridview.

Thanks.
stylelyfAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Easwaran ParamasivamCommented:
Given details are not clear enough to understand your problem. Could you post your code snippet here?
0
stylelyfAuthor Commented:
I have two datagridview.

Datagridview1 is populated with

Private Sub GetData(ByVal sp As String)

		connection = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("mainconnectionstring").ConnectionString)
		If connection.State = ConnectionState.Open Then
			connection.Close()
		End If

		connection.Open()

		command = New SqlCommand(sp, connection)
		command.CommandType = CommandType.StoredProcedure
		command.CommandTimeout = 0
		adapter = New SqlDataAdapter(command)
		Dim cb As New SqlCommandBuilder(adapter)
		adapter.UpdateCommand = cb.GetUpdateCommand(True)
		adapter.DeleteCommand = cb.GetDeleteCommand(True)
		adapter.InsertCommand = cb.GetInsertCommand(True)

		datatable = New DataTable()
		adapter.FillSchema(datatable, SchemaType.Mapped)
		adapter.Fill(datatable)


		' Refresh grid.
		BindingSource1.DataSource = datatable

		DataGridView1.DataSource = BindingSource1.DataSource

		For Each column As DataColumn In DirectCast(DataGridView1.DataSource, DataTable).Columns
			If column.DataType.ToString() = "System.Decimal" Then
				DataGridView1.Columns(column.ColumnName).DefaultCellStyle.Format = "N2"
			End If
		Next

		Try
			If Not IsNothing(DataGridView1.Columns("recid")) Then
				DataGridView1.Columns("recid").Visible = False
			End If
		Catch ex As Exception
			'
		End Try
	End Sub

Open in new window


Datagridview2 is loaded with

Private Sub GetData2(ByVal sp As String)

		connection = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("mainconnectionstring").ConnectionString)
		If connection.State = ConnectionState.Open Then
			connection.Close()
		End If

		connection.Open()

		command = New SqlCommand(sp, connection)
		command.CommandType = CommandType.StoredProcedure
		command.CommandTimeout = 0
		adapter2 = New SqlDataAdapter(command)
		Dim cb As New SqlCommandBuilder(adapter2)
		adapter2.UpdateCommand = cb.GetUpdateCommand(True)
		adapter2.DeleteCommand = cb.GetDeleteCommand(True)
		adapter2.InsertCommand = cb.GetInsertCommand(True)

		datatable2 = New DataTable()
		adapter2.FillSchema(datatable2, SchemaType.Mapped)
		adapter2.Fill(datatable2)


		' Refresh grid.
		BindingSource2.DataSource = datatable2

		DataGridView2.DataSource = BindingSource2.DataSource



		For Each column As DataColumn In DirectCast(DataGridView2.DataSource, DataTable).Columns
			If column.DataType.ToString() = "System.Decimal" Then
				DataGridView1.Columns(column.ColumnName).DefaultCellStyle.Format = "N2"
			End If
		Next

		Try
			If Not IsNothing(DataGridView1.Columns("recid")) Then
				DataGridView2.Columns("recid").Visible = False
			End If
		Catch ex As Exception
			'
		End Try

	End Sub

Open in new window


Both datagridview are on side-by-side and I have a "add to right" button and "remove button.
When I click add to right button, I copy the selected row in datagridview1 to the datagridview2. But since datagridview2 is bound, it says you cannot add a row to a bound datagridview.

Now looping through each cell from the datagridview1 is ok, but I think there is a clone function (I'm not sure if that is the one I can use) that I can use because I have too many rows.

Can you help me how I can copy the left grid row to the right? If possible without naming each columns just like function clone()?
Thanks.
0
stylelyfAuthor Commented:
Correction:

Now looping through each cell from the datagridview1 is ok, but I think there is a clone function (I'm not sure if that is the one I can use) that I can use because I have too many columns...
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

stylelyfAuthor Commented:
I tried this

		For Each row As DataGridViewRow In DataGridView1.SelectedRows
			DataGridView2.Rows.Add(row.DataBoundItem)
		Next

Open in new window


but it says

"Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound"
0
stylelyfAuthor Commented:
After so many hours of scratching head, I soon figure it out

For Each row As DataGridViewRow In DataGridView1.SelectedRows

Dim _row As DataRow = DirectCast(BindingSource1.Item(row.Index), DataRowView).Row
_row.Item("col1") = "value1"
_row.Item("col2") = "value2"
_row.Item("col3") = "value3"
next

adapter.Update(datatable)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stylelyfAuthor Commented:
No answer provided
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.