0

Aim: to access cells from a datagrid selected row. where the datacontext of the datagrid is a Linqtosql Query_result (i.e. not a well defined structure):

note: Query is across 3 Tables

displayed wpf form shows datagrid colums as follows:

| 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: Have reviewed many examples showing well defined DataGrids with known class definitions in DataContext,

Ian Finlay
  • 121
  • 2
  • 9

1 Answers1

0

SOLVED

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

ref: WPF Datagrid Get Selected Cell Value

Note: public static string getDataGridValueAt(DataGrid dGrid, string columnName) doesnt work on this link

Life should be easier than this!

   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;
        }
Community
  • 1
  • 1
Ian Finlay
  • 121
  • 2
  • 9