从数据网格中获取int值c#wpf [duplicate]

问题描述 投票:0回答:1

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

value不是字符串。

enter image description here

我该怎么办?

c# wpf datagrid wpfdatagrid
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.