【问题标题】:WPF VS2015 Accessing Datgrid cells when datagrid datacontext is query result当datagrid datacontext是查询结果时WPF VS2015访问Datgrid单元格
【发布时间】:2017-01-30 00:44:14
【问题描述】:

目标:从数据网格选定的行中访问单元格。其中数据网格的数据上下文是一个 Linqtosql Query_result(即不是一个明确定义的结构):

注意:查询跨越 3 个表

显示的 wpf 表单显示数据网格列如下:

| ftid |名称 | ftcft_id | ftcc_Id | ClassMaxVol |

 var queryR = from ft in dc.FluidTypes
                         from ftc in dc.FluidTypeClasses
                         from c in dc.Classes
                         where (ft.Id == ftc.FluidType_Id) &&
                                (ftc.Class_Id == c.Id)
                         select new { ft_id = ft.Id, ft_Name = ft.Name, 
                                      ftc_ft_id = ftc.FluidType_Id, 
                                      ftc_c_Id = ftc.Class_Id, 
                                      Class_MaxVol = c.MaxVolume };

            DataGridR.DataContext = dc;
            DataGridR.ItemsSource = null;
            DataGridR.ItemsSource = queryR;

[<DataGrid x:Name="DataGridR">
     <!-- Set data contect to dc-->
     <!-- Set item source to query result relative to data context-->
</DataGrid>][1]

PS:已经查看了许多示例,这些示例显示了在 DataContext 中具有已知类定义的定义良好的 DataGrid,

【问题讨论】:

    标签: c# wpf linq datagrid


    【解决方案1】:

    已解决

    谢谢https://stackoverflow.com/users/4998320/trikasus

    参考:WPF Datagrid Get Selected Cell Value

    注意:公共静态字符串 getDataGridValueAt(DataGrid dGrid, string columnName) 在此链接上不起作用

    生活应该比这更容易!

       public static string getDataGridColumnNameAt(DataGrid dGrid, int columnIndex)
            {
                string sRet = "";
                try
                {
                    if (dGrid.SelectedItem == null)
                    {
                        sRet = "";
                    }
                    else
                    {
                        string str = dGrid.SelectedItem.ToString(); // Take the selected line
                        str = str.Replace("}", "").Trim().Replace("{", "").Trim(); // Delete useless characters
                        if (columnIndex < 0 || columnIndex >= str.Split(',').Length) // case where the index can't be used 
                        {
                            sRet = "";
                        }
                        else
                        {
                            str = str.Split(',')[columnIndex].Trim();
                            str = str.Split('=')[0].Trim();
                            sRet = str;
                        }
                    }
                }
                catch (Exception ex)
                {
                    string sEx = ex.ToString();
                }
                return sRet;
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多