我有一个数据网格来显示来自数据库的数据,数据来自两个使用外键的表。但问题是数据没有显示在网格中,但行正在显示,但没有数据的行是空的。谁能指出我错的地方
<DataGrid AutoGenerateColumns="False" Name="SParts_grid" HorizontalAlignment="Center" Margin="32,127,32,0" VerticalAlignment="Top" Height="161" Width="530" >
<DataGrid.Columns>
<DataGridTextColumn Header="Part Code" Width="89" Binding="{Binding Path=SPartCode, Mode=OneWay}" />
<DataGridTextColumn Header="Part Name" Width="140" Binding="{Binding Path=SPartName, Mode=OneWay}" />
<DataGridTextColumn Width="90" Header="Part Price" Binding="{Binding Path=SPartSalePrice, Mode=OneWay}" />
<DataGridTextColumn Header="Model" Width="90" Binding="{Binding Path=ModelName, Mode=OneWay}" />
<DataGridTextColumn Header="Location" Width="60" Binding="{Binding Path=SPartLocation, Mode=OneWay}" />
<DataGridCheckBoxColumn Header="Active" Width="58" Binding="{Binding Path=SPartActive, Mode=OneWay}" />
</DataGrid.Columns>
</DataGrid>
所以我用一个类将数据库数据分配给类
private void Window_Loaded(object sender, RoutedEventArgs e)
{
LoadParts();
}
public class PartsListObjects
{
public int partid;
public string SPartName;
public string SPartCode;
public string SPartLocation;
public bool SPartActive;
public string ModelName;
public string SPartSalePrice;
}
private void LoadParts()
{
RST_DBDataContext conn = new RST_DBDataContext();
List<PartsListObjects> AllParts = (from s in conn.TblSpareParts
join m in conn.TblBikeModels on s.ModelID equals m.ModelID
select new PartsListObjects() { SPartName = s.SPartName, SPartCode = s.SPartCode, SPartLocation = s.SPartLocation, SPartSalePrice = s.SPartSalePrice.ToString(), ModelName = m.ModelName, SPartActive = s.SPartActive }).ToList();
SParts_grid.ItemsSource = AllParts;
SParts_grid.Items.Refresh();
}