我通过msdn a 创建了两个 datagridviews 现在我需要从“detailsDataGridView”获取数据表,但我无法转换它。错误:无法将 System.Windows.Forms.BindingSource 强制转换为 System.Data.DataTable 类型。
任何想法?代码,我试过了。
DataTable d = detailsDataGridView.DataSource as DataTable;
/////////////
DataTable dt = new DataTable();
dt = (DataTable)detailsDataGridView.DataSource;
////////////
BindingSource bs = new BindingSource();
bs.DataSource = detailsDataGridView.DataSource;
DataTable d = (DataTable)(bs.DataSource);
////////////
DataTable data = GetDataTableFromDGV(dgvMyMembers);
private DataTable GetDataTableFromDGV(DataGridView dgv)
{
var dt = ((DataTable)dgv.DataSource).Copy();
foreach (DataGridViewColumn column in dgv.Columns)
{
if (!column.Visible)
{
dt.Columns.Remove(column.Name);
}
}
return dt;
}