Question

Right now, I'm taking all the information under my tables and binding it to my DataGrids.

However, this leaves my foreign key visible on the DataGrid which I don't want. Also, I don't an Id column visible too.

How can I just select certain columns from my database and bind it onto my DataGrid instead of binding all my data onto my DataGrid?

I think I just have to write a query but seems like it's not working. Perhaps it's because I'm still new to Entity Framework.

Here's how I'm storing my data into db...:

 using (var db = new DMIDataContext())
{LotInformation newLot = new LotInformation();

        newLot.Id = lot.Id;
        newLot.lot_number = lot.lot_number;
        newLot.exp_date = lot.exp_date;

  foreach (Components comp in lot.Components)
        {

                   newLot.Components.Add(comp);

         }
         ComponentsList = newLot.Components;

  foreach (Families fam in lot.Families)
        {

                   newLot.Families.Add(fam);

         }
         Families = newLot.Families; 

         db.LotInformation.Add(newLot);
         db.SaveChanges();

Here's how I'm grabbing db data:

 public static void ReadLot(string lotNumber)
    {

        using (var db = new DMIDataContext())
        {
            try
            {
                LotInformation lotInfo = db.LotInformation.FirstOrDefault(r => r.lot_number.Equals(lotNumber));
            }
            catch (InvalidOperationException e)
            {

            }
        }
    }

I think the problem lies in the above query... I tried using FirstOrDefault().Select() but I guess I can't do a select Select after FirstOrDefault... not sure why.

The way the foreign keys are set up in my class is:

    public virtual int LotInformationId { get;  set; }
    public virtual LotInformation LotInformation { get; set; }

But I don't want that data binding to my dataGrid...

Hope this is clear. Please ask me if you have any questions.

Thanks!

Was it helpful?

Solution

Like Michael Perrenoud said, you can specify the columns you want in xaml:

  <DataGrid ItemSource="{Binding YourCollection}" SelectedItem="{Binding YourCurrentSelectedItem}">
      <DataGrid.Columns>
         <DataGridTextColumn Header="SomeColumnHeader" Binding={Binding SomePropertyOnTheModel} />
      </DataGrid.Columns>
  </DataGrid>

OTHER TIPS

If there are certain columns you don't want, you'll need to setup the columns manually and set their data binding rather than allowing the auto columns property to work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top