Solved

DUPLICATE A ROW TO ANOTHER ROW OF  A DATAGRIDVIEW

Posted on 2011-10-01
6
261 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

11 Experts available now in Live!

Get 1:1 Help Now