EnumerableRowCollection<DataRow> dataTableQuery = from row in agricultureData.SourceDataTable.AsEnumerable()
where row.Field<string>("AccountName").Trim() == "ATF"
orderby row.Field<string>("FutureCode")
select row;
I then create a DataTableView, a new BindingSource, and then set the DataSource of the BindingSource to the DataTableView:
var dataTableView = dataTableQuery.AsDataView();
bindingSource = new BindingSource();
bindingSource.DataSource = dataTableView;
dataGridView.DataSource = bindingSource;
Then, after a button is pressed on a form to update the SQL Server database that the agricultureData.SourceDataTable DataTable is from, the following code is called (by passing the BindingSource:
public void DataAdapterUpdate(ref BindingSource bindingSource)
{
SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
sqlCommandBuilder.GetUpdateCommand();
sqlDataAdapter.UpdateCommand = sqlCommandBuilder.GetUpdateCommand();
switch (bindingSource.DataSource.ToString())
{
case "":
sqlDataAdapter.Update((DataTable)bindingSource.DataSource);
break;
case "System.Data.LinqDataView":
// requires cast fixing..?
sqlDataAdapter.Update(bindingSource.DataSource as DataTable);
break;
}
}
Now, this code works fine for a BindingSource that is a standard DataTable.
But it doesn't work for the System.Data.LinqDataView. Is there any way to get the SqlDataAdapter to update the database table, based on a change to the LinqDataView?
I have tried 'bindingSource.DataSource as DataTable', but the following error occurs: 'Value cannot be null.'
Thanks,
Damian.