Reputation: 3851
in my application I used visual studio 2012, MSSQL server 2012, wpf and linq. In there i have to populate a datagrid with data from a database. my datagrid is as below.
<DataGrid HorizontalAlignment="Left" Height="189" Margin="10,10,0,0" VerticalAlignment="Top" Width="351" Name="StudentGrid" SelectionChanged="StudentGrid_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="Student ID" Binding="{Binding Path=Student.StudentId}"/>
<DataGridTextColumn Header="First Name" Binding="{Binding Path=Student.FirstName}"/>
<DataGridTextColumn Header="Last Name" Binding="{Binding Path=Student.LastName}"/>
<DataGridTextColumn Header="Gender" Binding="{Binding Path=Student.Gender}"/>
</DataGrid.Columns>
</DataGrid>
and my widow loaded method is as below.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SchoolDataDataContext con = new SchoolDataDataContext();
List<Student> students = (from s in con.Students
select s).ToList<Student>();
StudentGrid.ItemsSource = students;
}
But when the program is executed the datagrid does not show single data although the student table in the database contain data.
how can I fix this.
ok I changed the datagrid as mentioned below answers as below but nothing changes.
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="191" Margin="23,24,0,0" VerticalAlignment="Top" Width="447" Name="StudentGrid" >
<DataGrid.Columns>
<DataGridTextColumn Header="Student ID" Binding="{Binding Path=StudentId}"/>
<DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName}"/>
<DataGridTextColumn Header="Last Name" Binding="{Binding Path=LastName}"/>
<DataGridTextColumn Header="Gender" Binding="{Binding Path=Gender}"/>
</DataGrid.Columns>
</DataGrid>
Upvotes: 0
Views: 865
Reputation: 1316
Only use ItemsSource={Binding} if you have set the DataContext, which you have not.
This looks like it could be cause by 1 of 2 things.
<DataGridTextColumn Header="Student ID" Binding="{Binding Path=StudentId}"/>
<DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName}"/>
<DataGridTextColumn Header="Last Name" Binding="{Binding Path=LastName}"/>
<DataGridTextColumn Header="Gender" Binding="{Binding Path=Gender}"/>
If you have set your ItemsSource to the Generic list you wanted, you only need to set Path to the member name, not the Object name. like Above..
Check if your members in the Student class is public, they need to be in order for this to work.
and last check if your list is empty just so you know that's not the problem.
Upvotes: 1
Reputation: 872
First of all you have to assign the same datacontext to grid that you are using i.e SchoolDataDataContext, then you should set the items source (Also make sure that you have records in your itemsource)
Here is a complete guide
Upvotes: 0
Reputation: 222522
You should use ItemsSource="{Binding}" and AutoGenerateColumns="False"
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="189" Margin="10,10,0,0" VerticalAlignment="Top" Width="351" Name="StudentGrid" SelectionChanged="StudentGrid_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="Student ID" Binding="{Binding Path=Student.StudentId}"/>
<DataGridTextColumn Header="First Name" Binding="{Binding Path=Student.FirstName}"/>
<DataGridTextColumn Header="Last Name" Binding="{Binding Path=Student.LastName}"/>
<DataGridTextColumn Header="Gender" Binding="{Binding Path=Student.Gender}"/>
</DataGrid.Columns>
</DataGrid>
Upvotes: 2
Reputation: 937
You can try using ItemsSource="{Binding}"
in your xaml code of your datagrid.
Upvotes: 0