2017-01-30 9 views
0

Цель: доступ к ячейкам из выделенной строки данных. где DataContext в сетке данных является LinqToSql Query_result (т.е. не хорошо определена структура):WPF VS2015 Доступ к ячейкам Datgrid, когда datagrid datacontext - результат запроса

Примечание: Запрос находится через 3 Таблицы

отображаемых МОФ форме показывает Datagrid colums следующим образом:

| ftid | ftName | 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: Были рассмотрены многочисленные примеры, показывающие, хорошо определенные DataGrids с определениями известных классов в DataContext,

ответ

0

решаемые

Благодарности https://stackoverflow.com/users/4998320/trikasus

исх: WPF Datagrid Get Selected Cell Value

Примечание: public static string getDataGridValueAt (DataGrid dG rid, 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; 
     }