0

I want to get value from datagrid

I use this code

 if (Convert.ToString((datagrid_customer.SelectedCells[3].Column.GetCellContent(datagrid_customer.SelectedItem) as TextBlock).Text) == Convert.ToString((datagrid_customer.SelectedCells[1].Column.GetCellContent(datagrid_customer.Items[i]) as TextBlock).Text))
                { 
...
}

and it is work but show me string .

when i convert it to int i got error

Mah m = database.Mahs.FirstOrDefault(x => x.MahID == int.Parse((datagrid_customer.SelectedCells[0].Column.GetCellContent(datagrid_customer.Items[i]) as TextBlock).Text.Trim()));

error

System.NotSupportedException: 'LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression.'

value is not string .

enter image description here

What sould i do?

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
emadshn
  • 25
  • 7

1 Answers1

1

Try splitting it to separate operations:

TextBlock tb = datagrid_customer.SelectedCells[0].Column.GetCellContent(datagrid_customer.Items[i]) as TextBlock;
// null check
if(tb == null) return;

int i;
bool success = int.TryParse(tb.Text.Trim(), out i);
if(success)
  Mah m = database.Mahs.FirstOrDefault(x => x.MahID == i);
Clemens
  • 123,504
  • 12
  • 155
  • 268
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69