Home » C# ProgrammingRSS

CollectionViewSource Source = Linq to SQL

Hi, I am trying to bind data in my WPF Application using CollectionViewSource and LINQ to SQL. I have the following code in my XAML Where dbml:VendorsDataContext is my dbml file. In the code behind i have written the following code: addressViewSource.Source = bpo.GetVendorAddrTable(_vendorID);

Where GetVendorAddrTable has the following code:

var addr = from a in vendorsDC.tblVendor_Addresses

where a.vendorID == _vendorID

select new BussinessHelper.AddressContacts

{ VendorID = a.vendorID, Address1 = a.address1, Address2 = a.address2, City = a.tblVendor_City.cityName, State = a.tblVendor_State.stateName, Country = a.tblVendor_Country.countryName, VendorAddrID = a.vendorAddressID, CompleteAddr = a.address1 + a.address2 + a.tblVendor_City.cityName + a.tblVendor_State.stateName + a.tblVendor_Country.countryName

                       }; 
 
 
            return addr; 

When i run my application there is no data being bound. The CollectionViewSource source is null when i checked with code behind. Any ideas how to correct this?

 

5 Answers Found

 

Answer 1

Hi,

Apply your datasource like this.

private ListCollectionView QuoteDocumentCollectionView

        {

            get

            {

                return (ListCollectionView)CollectionViewSource.GetDefaultView(Data);

            }

        } 

Here data  is my source  of Data. it Works fine with me.Hope this will help you.

Thanks,

Rajnikant

 

Answer 2

Hi Rajnikant,

Thanks for the Code.

But when i databound in my code  behind to my listcollectionview i get the following error message:

abcCollectionViewSource.Source = listCollectionViewData i get the following error message

'System.Windows.Data.ListCollectionView' is not a valid value for property 'Source'."

 

Answer 3

Hi,

You dont have to give Source.

QuoteDocumentCollectionView.Filter = FilterQuoteDocuments;

 

and here is code  that filter your data.

 private bool FilterQuoteDocuments(object doc)

        {

            if (ShowAllFiles == true)

                return true;

            else

                return (doc as QuoteDocument).IsLatestVersion;

        }

Thanks,

Rajnikant

 

Answer 4

Hi NewtoLinq,

>When i run  my application  there is no data  being bound. The CollectionViewSource source  is null  when i checked  with code  behind. Any ideas  how to correct  this?

Based on my understanding, addressViewSource is a CollectionViewSource object, and you set the Source property with a query result from a Linq query. This should work, and I have made a simple test for that, and the source shouldn’t be null when you check with code behind. Maybe the bpo.GetVendorAddrTable method has returned a null value.

You can provide more information about it, so that we can have a better understanding about it.

Thanks,
Kevin Pan

 

Answer 5

@Rajnikant,@Kevin,

I probably did not put my question correctly.

I am trying to bind  this addressViewSource with a WPF Custom Combo Box as in the following link, except i intend to use LINQ to SQL instead of datasets.

http://www.codeproject.com/KB/WPF/WPFCustomComboBox.aspx?msg=3600593#xx3600593xx

I have a data  Access Layer which has a dbml  file.

I am trying to access this in the Presentation Layer using the following code  in XAML:

 

 

 

<dbml:VendorsDataContext x:Key="vendorAddress1"/>

 

 

<CollectionViewSource x:Key="addressViewSource1" Source="{Binding Path = tblVendor_Address ,Source={StaticResource vendorAddress1}}"/>

 

 

<mccmb:CustDataGridComboBoxColumn SelectedValueBinding="{Binding vendorAddressID}" Header="Vendor Address"

 

SelectedValuePath="vendorAddressID"

 

DisplayMemberPath="address1"

 

ItemsSource="{Binding Source={StaticResource addressViewSource1}}">

 

 

<my:DataGridTextColumn Header="Address1" Binding="{Binding address1, Mode=Default}" />

 

 

<my:DataGridTextColumn Header="Address2" Binding="{Binding address2, Mode=Default}" />

 

 

</mccmb:CustDataGridComboBoxColumn>

Where VendorsDataContext refes to my DBML file  which has the table tblVendor_Address.

 

In the code behind when i set

System.Windows.Data.

CollectionViewSource addressViewSource1 = ((System.Windows.Data.CollectionViewSource)(this.FindResource("addressViewSource1")));

It helps me set the source  and i get the result rows(data exists) but then i get the following error " Specified Cast does not exist" in the method

PrepareCellForEdit in the class

class

 

CustDataGridComboBoxColumn (Line 131:

object obj = ((DataRowView)editingElement.DataContext).Row[this.SelectedValuePath];)

So if you guys can help me understand how i can i can make LINQ to SQL work for this example and help me fix this problem i would appreciate that.

Thank You.

 

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter