Kaleet
Kaleet

Reputation: 91

DataGridView not displaying values using LINQ

It's very simple for the user.

They select the type of part, manufacturer from a ComboBox, and search the part code in a text box. Click search and the results return in a DataGridView.

Code:

var mType = CmbType.SelectedItem.ToString();
var mManufacturer = CmbMfr.SelectedValue.ToString();
var mCode = Convert.ToString(TxtProductCode.Text);

switch (mType)
{
    case "Faucets":
        var faucets = Resources.Accessor.SearchFaucets(mManufacturer, mCode);
        DgInventory.DataSource = faucets;
        break;
    case "Parts":
        var parts = Resources.Accessor.SearchParts(mManufacturer, mCode);
        DgInventory.DataSource = parts;
        break;
}

Accessor Code:

public static List<TblFaucets> SearchFaucets(string mId, string mCode)
{
    var dataConnect = new PxLinqSqlDataContext();

    return (from f in dataConnect.GetTable<TblFaucets>()
            where (f.Mfr == Convert.ToInt32(mId))
            where (f.Code == mCode)
            select f).ToList<TblFaucets>();
    }

What "messes up" is the results:

Upvotes: 1

Views: 370

Answers (1)

Pilgerstorfer Franz
Pilgerstorfer Franz

Reputation: 8359

First, PXDB.TblManufacturers seems to be a relation from tblFaucets to tblManufactureres. Mfr seems to be the foreignKey value within your faucets table refering to a manufacturer.

You may try creating an anonymous type holding only those data you want to. Within your select clause pick your data - as well as any relational data.

public static List<DisplayFaucet> SearchFaucets(string mId, string mCode)
{
    var dataConnect = new PxLinqSqlDataContext();

    return (from f in dataConnect.GetTable<TblFaucets>()
        where (f.Mfr == Convert.ToInt32(mId))
        where (f.Code == mCode)
        select new DisplayFaucet () {                              // create anonymous object 
                  ID = f.ID,                      // only holding the data you want to
                  Manufacturer = Manufacturer.Name,  // assuming there is property Name within your manufacturer table?!
                  Code = f.Code,
                  Description = f.Description,
                  Price = f.Price,
                  Date = f.Date
               }).ToList();
}

Add another class to hold your data to display

public class DisplayFaucet
{
    public int ID { get; set; }
    public string Manufacturer { get; set; }
    public string Code { get; set; } // check type
    public string Description { get; set; }
    public doublePrice{ get; set; } // check type
    public DaetTime Date { get; set; } // check type
}

Note that SearchFaucets(..) may no longer return items of type tblFaucet! Instead I created a new class. This one contains all data which should be displayed!

Upvotes: 1

Related Questions