0

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.

enter image description here

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>
4
  • 1
    Do you have Items inside Students? Commented Mar 31, 2014 at 5:59
  • yep. I entered some data. also I developed add student functionality. so the table contains data.... Commented Mar 31, 2014 at 6:01
  • 2
    Not the table, your Students list while debugging Commented Mar 31, 2014 at 6:04
  • oh. it was just a problem with my data base connection. :( Commented Mar 31, 2014 at 15:43

4 Answers 4

2

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

You can try using ItemsSource="{Binding}" in your xaml code of your datagrid.

Comments

0

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

Guide1 Guide2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.