【问题标题】:get int value from data grid c# wpf [duplicate]从数据网格c#wpf中获取int值
【发布时间】:2019-08-16 12:41:06
【问题描述】:

我想从数据网格中获取价值

我用这个代码

 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)' 方法,并且该方法无法转换为存储表达式。'

值不是字符串。

我该怎么办?

【问题讨论】:

    标签: c# wpf datagrid wpfdatagrid


    【解决方案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);
    

    【讨论】:

    • out 、 int itryparse 上显示错误
    • @emadshn 检查已编辑的答案
    • 再次在 tryparse 和 out i 上出错
    • 它的工作。谢谢兄弟。
    猜你喜欢
    • 2017-11-16
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 2016-02-17
    相关资源
    最近更新 更多