Link to home
Start Free TrialLog in
Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

Avatar of p_davis
p_davis

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).
are both the datagridview and the datatable have exactly the same definitions (same columns)?
Avatar of RIAS

ASKER

Yes
Avatar of RIAS

ASKER

Dim dt As DataTable = DirectCast(Dg.DataSource, DataTable).Clone()
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

what are you trying to do? Duplicate the rows?
Avatar of RIAS

ASKER

Yes duplicate the selected rows
Avatar of RIAS

ASKER

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.
Avatar of RIAS

ASKER

on this code:
  If IsDBNull(row(0).Value) = False AndAlso (row(0).Value) = True Then
try this:

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

Open in new window

Avatar of RIAS

ASKER

How can I add the clonerows to datable?
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RIAS

ASKER

Thanks Eric,
Will try and get back
Avatar of RIAS

ASKER

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
Avatar of RIAS

ASKER

Any suggestion on
  dt.ImportRow(DirectCast(Dg.DataSource, DataTable).Rows(row.Index))
to be dataview than datatable
Avatar of RIAS

ASKER

Thanks Eric!
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.