从数据网格c#wpf获取int值

时间:2019-03-26 07:37:40

标签: c# wpf datagrid wpfdatagrid

我想从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)'方法,并且该方法无法转换为商店表达式。'

值不是字符串。

enter image description here

我该做什么?

1 个答案:

答案 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);