Showing posts with label datagridview. Show all posts
Showing posts with label datagridview. 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

Monday, November 3, 2008

Handling Keystrokes in DataGridView

Private Sub dgv_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgv.EditingControlShowing
 
If Me.dgv.CurrentCell.ColumnIndex = 0 And Not e.Control Is Nothing Then
    Dim tb As TextBox = CType(e.Control, TextBox)
    AddHandler tb.KeyDown, AddressOf TextBox_KeyDown
    AddHandler tb.KeyPress, AddressOf TextBox_KeyPress
End If

 
End Sub
 

Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
 If  e.KeyCode = Keys.Space Then
     flag = True
 End If

End Sub
 
Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
 e.Handled = flag
 flag = False

End Sub