Solved

DUPLICATE A ROW TO ANOTHER ROW OF  A DATAGRIDVIEW

Posted on 2011-10-01
6
262 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now