vini
vini

Reputation: 4732

where on LINQ with DataTable

var results = from myRow in dsPac.AsEnumerable()
              where myRow.Field<string>("Package_Name").Equals(lblPackageName.Text)
              select myRow;

The dsPac contains

enter image description here

I want to select data corresponding to Package1712200466 ie

enter image description here

When I bind to a GridView I get the following error

DataBinding: 'System.Data.DataRow' does not contain a property
with the name 'Holiday_ID'.

Upvotes: 1

Views: 1819

Answers (2)

Phil
Phil

Reputation: 42991

Alternatively you can use AsDataView() to convert the row collection back to a DataView which the GridView will be able to bind to.

var results = (from myRow in dsPac.AsEnumerable()
              where myRow.Field<string>("Package_Name").Equals(lblPackageName.Text)
              select myRow).AsDataView();

Upvotes: 3

theMayer
theMayer

Reputation: 16167

In the past when I have data rows that I wish to bind into a GridView, I create an object to do so. In this case, you could probably use your Linq query to create an anonymous object that will do the trick.

 var results = from myRow in dsPac.AsEnumerable()
               where myRow.Field<string>("Package_Name").Equals(lblPackageName.Text)
               select new { Holiday_ID = myRow["Holiday_ID"],
                            Holiday_Description = myRow["Holiday_Description"],
                            Holiday_Date = myRow["Holiday_Date"] };

I don't believe biding to the row directly will work - see the exception text. Row columns are accessed via an Item[] indexer, so you would need to write a WPF converter to achieve the same effect. In all, a waste of time because the Linq query will work.

Upvotes: 5

Related Questions