Solved

Add row in datatable

Posted on 2016-09-13
18
52 Views
Last Modified: 2016-09-14
Hello ,
How do I add the row to a datatable

 For Each row As DataGridViewRow In Dg.Rows
                    If IsDBNull(Dg.Rows(row.Index).Cells(0).Value) = False AndAlso (Dg.Rows(row.Index).Cells(0).Value) = True Then
                        dt.Rows.Add(row.)----error
                    End If
                Next

Open in new window

0
Comment
Question by:RIAS
  • 10
  • 7
18 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 41796162
dt.Rows.Add(row.)----error
                              ^
                              |
the . after row needs to be removed -- what was the exact error (just in case this was a typo pasting here).
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41796180
are both the datagridview and the datatable have exactly the same definitions (same columns)?
0
 

Author Comment

by:RIAS
ID: 41796182
Yes
0
 

Author Comment

by:RIAS
ID: 41796189
Dim dt As DataTable = DirectCast(Dg.DataSource, DataTable).Clone()
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41796196
I would try this (not tested):

For Each row As DataRow In DirectCast(Dg.DataSource, DataTable).Rows
   If IsDBNull(row(0).Value) = False AndAlso (row.(0).Value) = True Then
      dt.Rows.Add(row)
   End If
Next

Open in new window

0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41796199
what are you trying to do? Duplicate the rows?
0
 

Author Comment

by:RIAS
ID: 41796206
Yes duplicate the selected rows
0
 

Author Comment

by:RIAS
ID: 41796214
Eric,
Got this eror on your code
An unhandled exception of type 'System.MissingMemberException' occurred in Microsoft.VisualBasic.dll

Additional information: Public member 'Value' on type 'Boolean' not found.
0
 

Author Comment

by:RIAS
ID: 41796215
on this code:
  If IsDBNull(row(0).Value) = False AndAlso (row(0).Value) = True Then
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41796221
try this:

If not IsDBNull(row(0)) AndAlso row(0) = True Then

Open in new window

0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41796303
0
 

Author Comment

by:RIAS
ID: 41796337
How can I add the clonerows to datable?
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 41796508
ok I have created something just for you:
Public Class Form1

    Private Function CreateDataSet() As DataSet
        Dim dsData As New DataSet("TestDataSet")

        Using dt As New DataTable("States")
            dt.Columns.Add("ID", GetType(Integer))
            dt.Columns.Add("Code", GetType(String))
            dt.Columns.Add("Description", GetType(String))
            dt.Columns.Add("Population", GetType(Integer))
            dt.PrimaryKey = New DataColumn() {dt.Columns("ID"), dt.Columns("IDParent")}

            dt.LoadDataRow(New Object() {1, "AB", "Alberta", 0}, True)
            dt.LoadDataRow(New Object() {2, "BC", "British Columbia", 0}, True)
            dt.LoadDataRow(New Object() {3, "MB", "Manitoba", 1162800}, True)
            dt.LoadDataRow(New Object() {4, "NB", "New Brunswick", 750600}, True)
            dt.LoadDataRow(New Object() {5, "QC", "Québec", 7487200}, True)

            dsData.Tables.Add(dt)
        End Using

        Return dsData
    End Function

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim ds As DataSet = CreateDataSet()
        DataGridView1.DataSource = ds.Tables(0)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim originalRow As DataRow = DirectCast(DataGridView1.CurrentRow.DataBoundItem, DataRowView).Row
        Dim dt As DataTable = DirectCast(DataGridView1.DataSource, DataTable)
        Dim newRow As DataRow = dt.NewRow()

        newRow.ItemArray = originalRow.ItemArray
        newRow("ID") = dt.Rows.Count + 1
        dt.Rows.Add(newRow)

        DataGridView1.DataSource = dt
    End Sub

End Class

Open in new window

0
 

Author Comment

by:RIAS
ID: 41796601
Thanks Eric,
Will try and get back
0
 

Author Comment

by:RIAS
ID: 41797430
Eric,

   Dim originalRow As DataRow = DirectCast(Dg.CurrentRow.DataBoundItem, DataRowView).Row

takes the row fronm the original datatable i.e datasource of the datagridview.But my datagridview is sorted and has datasource as dataview and not datatable.
How can i get the current row from dataview and not datatable.
Cheers
0
 

Author Comment

by:RIAS
ID: 41797463
Any suggestion on
  dt.ImportRow(DirectCast(Dg.DataSource, DataTable).Rows(row.Index))
to be dataview than datatable
0
 

Author Closing Comment

by:RIAS
ID: 41797468
Thanks Eric!
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41797573
That would have been good to know to start with! You surely have a key in your fields, Search for that key instead of relying to the row index.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

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

12 Experts available now in Live!

Get 1:1 Help Now