hello all,
i have Datagrid which show data from two different sql tables, like below:
<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:
var MyDG = (from DD in de.DevicesDets
join DT in de.DeviceTs
on DD.DDID equals DT.SN
where DT.EmpID == ID
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;