Click here to Skip to main content
14,931,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello all,
i have Datagrid which show data from two different sql tables, like below:

HTML
<DataGrid x:Name="MyDevicesDataGrid"
SelectionChanged="MyDevicesDataGrid_SelectionChanged" AutoGenerateColumns="False">
<DataGrid.Columns>
  <DataGridTextColumn Binding="{Binding SN}"         Header="SN"/>
  <DataGridTextColumn Binding="{Binding Material}"   Header="Material"/>
  <DataGridTextColumn Binding="{Binding DDOS}"       Header="OS"/>
  <DataGridTextColumn Binding="{Binding DDHW}"       Header="HardWare Version"/>
  <DataGridTextColumn Binding="{Binding DDAvilabil}" Header="Availability"/>
</DataGrid.Columns>
</DataGrid>


the first two columns belongs to table (A), the rest are belongs to Table (B).
the binding code as shown below:
C#
var MyDG = (from DD in de.DevicesDets
                          join DT in de.DeviceTs
                          on DD.DDID equals DT.SN
                          where DT.EmpID == ID // ID is string value
                          select new { DT.SN,DT.Material,DD.DDHW,DD.DDAvilabil,DD.DDOS}).ToList();
              MyDevicesDataGrid.ItemsSource = MyDG ;


The data is showing in the datagrid correctly.
i have another function triggered on MyDevicesDataGrid_SelectionChanged event
when item is selected i want to get the first value which is [SN] from Table (A).
what i did is create a class like below:

private string sN;
       private string material;
       private string dDHW;
       private bool dDAvilabil;
       private string dDOS;

       public string SN
       {
           get
           {
               return sN;
           }

           set
           {
               sN = value;
           }
       }

       public string Material
       {
           get
           {
               return material;
           }

           set
           {
               material = value;
           }
       }

       public string DDHW
       {
           get
           {
               return dDHW;
           }

           set
           {
               dDHW = value;
           }
       }

       public bool DDAvilabil
       {
           get
           {
               return dDAvilabil;
           }

           set
           {
               dDAvilabil = value;
           }
       }

       public string DDOS
       {
           get
           {
               return dDOS;
           }

           set
           {
               dDOS = value;
           }
       }
   }


and i am try to get the SN like this:
string id = (MyDevicesDataGrid.SelectedItem as DevicesDetails).SN;


but i get null result and error message:Object reference not set to an instance of an object.

any one knows how t do it

What I have tried:

string id = (MyDevicesDataGrid.SelectedItem as DevicesDetails).SN;

var selectedItem = MyDevicesDataGrid.SelectedItem as DevicesDetails;
Posted
Comments
Eric Lynch 19-Jun-18 10:44am
   
Your LINQ query appears to return a dynamic type; whereas, your assignment appears to expect a specific type (DevicesDetail). When you use the "as" clause, it will cast to the specified type (if the object is of that type); otherwise, it will yield null. You can't simply cast from a dynamic type to a specific type. Its not valid. You'll need to either alter your LINQ query to select your desired type or write a method to do the conversion yourself later.

ADDENDUM: To be a bit clearer, you can change "select new { … }" to "select new DevicesDetail { … }" and then change to assignments like "SN = DT.SN" within the brackets (where the "..." bits are).
Member 13199484 19-Jun-18 10:53am
   
Hello Mr. Eric
thanks for your comment, you are right, i am casting from dynamic to specific since the variable after (AS) which is (SN) has the same type on the two DB's tables and in the list (DevicesDetail).
however, i try to import the full selected row to object, its saved but i cannot extract the column that i need, same as:
var Item = MyDevicesDataGrid.SelectedItem ;
here i have all the row saved on "item", and if i want to get the first value i will write : (item.?), i got 4 options (get type, to string, ....).
actually i dont know what the best way to get the first cell value
Eric Lynch 19-Jun-18 11:03am
   
Sorry, accidentally replied to your question instead of your comment...


I'd recommend changing your LINQ query to yield a specific type as suggested in the original response.

While I would not recommend it, I think the following should also work, if you insist on doing it later.

string id = (MyDevicesDataGrid.SelectedItem as dynamic).SN;
Member 13199484 19-Jun-18 11:06am
   
I've tried it, it's returned a null result.
i will try to edit the LINQ query to specify the type.
thank you.
Eric Lynch 19-Jun-18 12:52pm
   
First, I really think specifying the type in your query is the best option. That said, assuming a grid view item was selected, I would expect the other suggestion to work.

When you say "it's returned a null", what is "it"?

Is "MyDevicesDataGrid.SelectedItem" null? If so, that means the user hasn't selected a data grid row in the UI?

Is "(MyDevicesDataGrid.SelectItem as dynamic)" null? If so, I'd use the debugger to examine "MyDevicesDataGrid.SelectedItem" to determine its underlying type.

Is "(MyDevicesDataGrid.SelectedItem as dynamic).SN" null? If this is the case, either the database column may be null or the compiler may be having some trouble resolving the dynamic property.

Both specificity, and the debugger, help when trying to diagnose these issues. That said, where possible, I prefer to simply work with a specific type for the end result of a LINQ query, to avoid all of the above complexities. I often use dynamic types, for inner portions of a LINQ query, but almost always select to a specific type for the outermost result.
Member 13199484 19-Jun-18 13:09pm
   
good explanation of all possibilities from you.
the below codes returned null
- MyDevicesDataGrid.SelectItem as dynamic
- (MyDevicesDataGrid.SelectedItem as dynamic).SN
i am agree with you about specifying the type.
i choose the dynamic way to avoid the missed synchronization between the two tables.
i skip a part of code to check each row in the first table with it's counterpart on the second table, because all the operation applied to one table and the second table import the data from the first table on specific event, so i cannot make sure the tables are matching completely, that is why i took the first two columns from the first table and the rest from the second table.
i did skip 3 lines of code to face a 3 meters gap.
i will correct the application's behavior.
thanks again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900