2016-07-06 72 views
0

我正在使用DataTable填寫我的DataGridView的信息。然後,我用的BindingSource使用過濾上的數據:現在如何將BindingSource轉換爲DataTable?

BindingSource bs = new BindingSource(); 
bs.DataSource = dgvAppList.DataSource; 
bs.Filter = ""; //some filter commands here 
dgvAppList.DataSource = bs; 

,過濾後,我想將數據從我的上一個DataTable的DataGridView存儲。我試着用下面的代碼:

var bindingSource = (BindingSource)dgvAppList.DataSource; 
var table = (DataTable)bindingSource.DataSource; // error pops out here 

但我總是最後得到的錯誤:

Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'.

我怎麼會投BindingSource的數據表來存儲數據表變量我的DataGridView的數據?

回答

0

您可以複製DataGridViewDataTable這樣的:

DataTable dt = new DataTable(); 
dt = Ctype(dataGridView1.DataSource,DataTable).copy();