简体   繁体   中英

Data is not displaying in datagrid wpf

i have a Data Grid to display the data coming form database, Data is coming from two tables using foreign key.But problem is that data is not displaying in the grid but rows are displaying but rows are empty without data. can any one point out my mistake where i am wrong

        <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>

So i used a class to assign the database data into the class

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();
}

You should binding to property, not to field.

Change your model class to following code:

public class PartsListObjects
{
    public int partid {get; set; }
    public string SPartName {get; set; }
    public string SPartCode {get; set; }
    public string SPartLocation {get; set; }
    public bool SPartActive {get; set; }
    public string ModelName {get; set; }
    public string SPartSalePrice {get; set; }
}

在Datagrid属性的xaml代码中将属性CanUserReorderColumns="false"true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM