Showing posts with label bindingsource to datatable. Show all posts
Showing posts with label bindingsource to datatable. Show all posts

Monday, June 15, 2015

vb.net - DataGridView to Datatable

Convert DataGridView Source to Datatable
***********************************

dim dt as New DataTable

dt = TryCast(dgv.DataSource, DataTable)

if, the datasource is BindingSource, then

use this:

dim bs as BindingSource

bs = TryCast(dgv.DataSource, BindingSource)
dim dv as New DataView
dv = bs.SyncRoot
dt = dv.ToTable


if, the Datasource is any of above, use this:


dt = New DataTable

Dim bs As New BindingSource
If TypeOf dgv.DataSource Is DataTable Then
 dt = TryCast(dgv.DataSource, DataTable)
Else
   bs = TryCast(dgv.DataSource, BindingSource)
   dv = New DataView
   dv = bs.SyncRoot
   dt = dv.ToTable
End If