Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 84
  • Last Modified:

Add row in datatable

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
RIAS
Asked:
RIAS
  • 10
  • 7
1 Solution
 
p_davisCommented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
are both the datagridview and the datatable have exactly the same definitions (same columns)?
0
 
RIASAuthor Commented:
Yes
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
RIASAuthor Commented:
Dim dt As DataTable = DirectCast(Dg.DataSource, DataTable).Clone()
0
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
what are you trying to do? Duplicate the rows?
0
 
RIASAuthor Commented:
Yes duplicate the selected rows
0
 
RIASAuthor Commented:
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
 
RIASAuthor Commented:
on this code:
  If IsDBNull(row(0).Value) = False AndAlso (row(0).Value) = True Then
0
 
Éric MoreauSenior .Net ConsultantCommented:
try this:

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

Open in new window

0
 
RIASAuthor Commented:
How can I add the clonerows to datable?
0
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
RIASAuthor Commented:
Thanks Eric,
Will try and get back
0
 
RIASAuthor Commented:
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
 
RIASAuthor Commented:
Any suggestion on
  dt.ImportRow(DirectCast(Dg.DataSource, DataTable).Rows(row.Index))
to be dataview than datatable
0
 
RIASAuthor Commented:
Thanks Eric!
0
 
Éric MoreauSenior .Net ConsultantCommented:
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 10
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now