Solved

DUPLICATE A ROW TO ANOTHER ROW OF  A DATAGRIDVIEW

Posted on 2011-10-01
6
263 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

772 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