Solved

DUPLICATE A ROW TO ANOTHER ROW OF  A DATAGRIDVIEW

Posted on 2011-10-01
6
264 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checking a checkbox based on SQL DataReader boolean value in ASP.NET(VB) 2 33
Visual Studios 1 77
get combo value in class 5 14
VB.NET Inline If statement 4 18
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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