简体   繁体   English

为什么datagrid不会在行中显示数据

[英]Why datagrid doesn't show data in rows

My datagrid doesn't show data in rows despite that I added data. 尽管我添加了数据,但我的数据网格不会显示行数据。 Car is list of id of cars. 汽车是汽车的id列表。

 DataGridTextColumn col1 = new DataGridTextColumn();
 myDataGrid.Columns.Add(col1);
 col1.Binding = new Binding("id");
 col1.Header = "ID";
 foreach (Car carr in car)
 {
     myDataGrid.Items.Add(carr.ToString());
 }

My xaml file 我的xaml文件

<DataGrid x:Name="myDataGrid" 
          HorizontalAlignment="Left" 
          Height="300" 
          Width="600"
          VerticalAlignment="Top" 
          IsReadOnly="True" 
          RenderTransformOrigin="0.52,0.47" 
          Margin="100,82,0,0" 
          FontSize="15" />

Set the ItemSource property of the DataGrid: 设置DataGrid的ItemSource属性:

    myDataGrid.ItemSource = car;

See https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.datagrid?view=netframework-4.8 请参阅https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.datagrid?view=netframework-4.8

for more information. 欲获得更多信息。

OR You can use xaml for custom columns: 或者您可以将xaml用于自定义列:

    <DataGrid x:Name="CARS" ItemsSource="{Binding Path=Car}" IsReadOnly="True" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="ID" Binding="{Binding Path=IdPropertyOfCarObject}"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

And in code behind set the data context to the class that has a list of car objects. 在后面的代码中,将数据上下文设置为具有汽车对象列表的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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