Ruchan
Ruchan

Reputation: 3194

set datasource of gridview to a datatable without bind

I am seting a datasource of a dataGridview to a table. First In Load Event

Datagridview.datasource=DTFromSQl

In some Random Event I do.

Dim Dt as datatable=DataGridview1.datasource

When I do DT.rows.clear() it also clears the rows in the Datagridview. I suppose its due to databinding. But how is the databind occuring? and how to remove it so that changes in DT occurs in it only.

Thanks


Ok I did it but, something doesnt feel right about it.

Dim DTSend As New DataTable

    For i As Integer = 0 To DataGridView1.ColumnCount - 1
        DTSend.Columns.Add(DataGridView1.Columns(i).Name)
    Next

Upvotes: 0

Views: 12082

Answers (1)

Vishal Gavle
Vishal Gavle

Reputation: 367

Use the following code. when you will clear the datatable it will not clear your datagridview

 Dim dtsend As DataTable

 dtsend = CType(DataGridView1.DataSource, DataTable).Copy()

 dtsend.Clear()

Upvotes: 2

Related Questions