We help IT Professionals succeed at work.
Get Started

Whats wrong with my LINQ query  Trying to Find a Record in List

112 Views
Last Modified: 2020-03-22
I have this Grid in my c# application

The Display Value in column 1  has an ID and Description  ( see  screen below)

Selected Record From Combo Box withing Grid

When i change values in column1 I want the description written into column2

I am trying to use LINQ  and a LAMDA Expression  . My script is as shown below

[private void dgvDetailsTable_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {

                switch (this.dgvDetailsTable.Columns[e.ColumnIndex].Name)
                {
                    case "dgvcmbarsaleditemid":
                        var result = ((List<InventoryItemModel>)dgvcmbarsaleditemid.DataSource).Find(x => x.INVITEMId.Equals(dgvcmbarsaleditemid.Selected));
                        this.dgvDetailsTable.CurrentRow.Cells[2].Value = result.INVITEMDescription;
                       

                        break;


                }
            }
        }[/code]

My database  field name is INVITEMId   and my   DataGrid  column 1 name is   dgvcmbarsaleditemid.


In the debugger  I can see that the 1188 records  were selected  and in the debugger i can see the loop search going on

See  Debugger  Screen below

Debugger Screen showing that I have ALL the 1188 records in my List

At the end of the looping it can not find the selected record and put it in the result variable even though the selected record
is on screen and coming from the database.  See  Error screen below

Error Screen with result  variable having null
Whats wrong with my LAMDA  expression  and how can i correct it

Thanks for any assistance

Olukay
Comment
Watch Question
Consultant
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE