BindingSource.Clear method

I

Isaac Abraham

Hiya,

I'm mucking around with .net 2, and experimenting with the BindingSource
control. I've set the datasource to a dataset and set the datamember
property as the table (or perhaps i've directly set the datasource to
the table and left the datamember as null). When I run the Clear()
method on the BindingSource, I get an exception from the runtime saying
that the clear method cannot be used on the underlying datasource.

Any ideas what I'm doign wrong?

Thanks
 
B

Bart Mermuys

Hi,

Isaac Abraham said:
Hiya,

I'm mucking around with .net 2, and experimenting with the BindingSource
control. I've set the datasource to a dataset and set the datamember
property as the table (or perhaps i've directly set the datasource to the
table and left the datamember as null). When I run the Clear() method on
the BindingSource, I get an exception from the runtime saying that the
clear method cannot be used on the underlying datasource.

Any ideas what I'm doign wrong?

I don't think you're doing anything wrong, it just can't be done. You have
to Clear the underlying DataTable instead.

HTH,
Greetings
 
B

Bart Mermuys

Hi,

Paul Gielens said:
See this
(http://msdn2.microsoft.com/library/system.windows.forms.bindingsource.clear.aspx)
example where the table is assigned to the BindingSource.DataSource
property. Internally the table its DataRow collection is bound to the
connector. The Clear operation now deletes all data rows from your table.

I don't see an example. And when you bind a DataTable then it's internally
bound to the DefaultView (DataView).

A DataTable isn't directly bindable because it doesn't implement IList, it
does implement IListSource which provides the DefaultView.

DataTable dt = new DataTable();
dt.Columns.Add( "test", typeof(string) );
BindingSource bs = new BindingSource(dt, "");
Console.WriteLine( bs.List.GetType() ); // prints DataView
Console.WriteLine( bs.List == dt.DefaultView ); // prints true


Greetings
 
P

Paul Gielens

This showed up while inspecting the DataView class with reflector:
void IList.Clear()
{
throw ExceptionBuilder.CanNotClear();
}public static Exception CanNotClear()
{
return
ExceptionBuilder._Argument(Res.GetString("DataView_CanNotClear"));
}

I was assuming that the ListDictionaryInternal collection class which is
being used inside the DataView class was capable of clearing its values, and
so it seems with reflector. What I overlooked is the DataView class relying
on the IBindingListView (inheriting IBindingList and IList) contract and
thus implements the Clear operation.

Sorry for the wrong link... this would be the correct one
http://msdn2.microsoft.com/library/system.windows.forms.bindingsource.list.aspx.
Hope this clears things up.

###
Best regards,
Paul Gielens

Visit my blog @ http://weblogs.asp.net/pgielens/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top