我已经通过将数据绑定到数据库列成功地在datagrid中显示数据,但问题是,所有数据列在完全显示之后出现两次即(id,user_id,status,id,user_id,status,)。除此之外,这是我的代码
<DataGrid x:Name="Grid_display" Grid.ColumnSpan="3" Margin="63,51,36,10" Grid.RowSpan="2">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Path=id}"/>
<DataGridTextColumn Header="Publisher ID" Binding="{Binding Path=user_id}"/>
<DataGridTextColumn Header="Publication Title" Binding="{Binding Path=title}"/>
<DataGridTextColumn Header="Published Date of Document" Binding="{Binding Path=published_date}"/>
<DataGridTextColumn Header="File" Binding="{Binding Path=file}"/>
<DataGridTextColumn Header="Status" Binding="{Binding Path=status}"/>
</DataGrid.Columns>
</DataGrid>
private void display_btn_Click(object sender, RoutedEventArgs e)
{
List<publication> q = (from p in db.publications where p.user_id == get_id select p).ToList();
Grid_display.ItemsSource = q;
}
答案 0 :(得分:8)
您必须在数据网格上设置AutoGenerateColumns="False"
。默认情况下,它是真的,并且正在添加另一组列。
<DataGrid x:Name="Grid_display" Grid.ColumnSpan="3" AutoGenerateColumns="False"
Margin="63,51,36,10" Grid.RowSpan="2"/>