Solved

DUPLICATE A ROW TO ANOTHER ROW OF  A DATAGRIDVIEW

Posted on 2011-10-01
6
266 Views
Last Modified: 2012-05-12
Hi All,

I want to duplication a row to another row of a datagridview.

Row Data
0      Fill with data
1      Fill with data
2      Empty Row

How could I copy row 0 to row 2 ?

Thank you.
0
Comment
Question by:emi_sastra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 19

Accepted Solution

by:
Shahan Ayyub earned 250 total points
ID: 36899527
Here is one way as i saw:

    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Me.DuplicateGridRow(0)  ' Pass row number (you want to duplicate) here.
    End Sub
    
    Public Sub DuplicateGridRow(ByVal RowIndex As Integer)
        Dim r As DataGridViewRow = New DataGridViewRow
        r = CType(dataGridView1.Rows(RowIndex).Clone,DataGridViewRow)
        For i As Integer = 0 to r.Cells.Count -1
            r.Cells(i).Value = dataGridView1.Rows(RowIndex).Cells(i).Value
        Next
        dataGridView1.Rows.Add(r)
    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 36900896
Hi Shahan,

The empty row was already there.
No need to add a new row, after copy then add a new empty row.

Thank you.

0
 
LVL 20

Expert Comment

by:ElrondCT
ID: 36901125
If you have a data-bound DataGridView, you should be changing rows in the underlying DataSource (usually a DataTable, though it could be different).
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 1

Author Comment

by:emi_sastra
ID: 36901215
Hi ElrondCT,

Yes, I have bound it to a datatable.
I don't get your point.

Thank you.
0
 
LVL 20

Assisted Solution

by:ElrondCT
ElrondCT earned 250 total points
ID: 36901229
OK, then you should be copying the data to a new row in the DataTable:

dim drNew as Table.Row
drNew = TableInstance.NewRow
drNew.firstfield = TableInstance(0).firstfield
... etc. ...
TableInstance.AddNewRow(drNew)

If row 2 already exists in the table, then skip the .NewRow command and instead do:

TableInstance(2).firstfield = TableInstance(0).firstfield
... etc. ...
0
 
LVL 1

Author Closing Comment

by:emi_sastra
ID: 36913955
Hi All,

Thank you very much for your hep.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question