I'm trying to cast the bindingdatasource to datatable using this code
BindingSource bs = (BindingSource)gvSideMember.DataSource;
DataTable tCxC = (DataTable)bs.DataSource;
throws error unable to cast bindingsource to datatable
then i tried this code
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;
}
it again show me same error
DataTable dt = new DataTable();
DataSourceSelectArguments args = new DataSourceSelectArguments();
DataView dv = new DataView();
dv = (DataView)SqlDataSource1.Select(args);
dt = dv.ToTable();
but i don't know what is the base class of DataSourceSelectArguments ? So I can't how can i do this cast?
This is my solution which also works if you are using BindingSource as n child level.
Looks like your
bs.DataSource
is actually anotherBindingSource
, so you can try this: