Search code examples
c#wpfdatagridwpfdatagrid

get int value from data grid c# wpf


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?


Solution

  • 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);