我想从datagrid获得价值
我使用此代码
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))
{
...
}
这是可行的,但请向我显示字符串。
当我将其转换为int时出现错误
Mah m = database.Mahs.FirstOrDefault(x => x.MahID == int.Parse((datagrid_customer.SelectedCells[0].Column.GetCellContent(datagrid_customer.Items[i]) as TextBlock).Text.Trim()));
错误
System.NotSupportedException:'LINQ to Entities无法识别方法'Int32 Parse(System.String)'方法,并且该方法无法转换为商店表达式。'
值不是字符串。
我该做什么?
答案 0 :(得分:1)
尝试将其拆分为单独的操作:
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);