none
Datagrid is not populated

    Question

  • Hi,

    I am developing a silverlight navigation framework application in VS2010.

    I have tried populating a datagrid with the business objects of a silverlight class(Student). But it is not done.

    One of my page called Overview which has the datagrid contains,

    <navigation:Page xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="InStepSM.SL.Views.Overview"

    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation

    xmlns:bureauBlack="clr-namespace:System.Windows.Controls.Theming;assembly=System.Windows.Controls.Theming.BureauBlack"

    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml

    xmlns:d=http://schemas.microsoft.com/expression/blend/2008

    xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006

    mc:Ignorable="d"

    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"

    d:DesignWidth="640" d:DesignHeight="480" Title="Overview Page">

    <bureauBlack:BureauBlackTheme>

    <Grid x:Name="LayoutRoot">

    <Grid.ColumnDefinitions>

    <ColumnDefinition Width="*" />

    <ColumnDefinition Width="639*" />

    </Grid.ColumnDefinitions>
    <TextBlock x:Name="HeaderText" Style="{StaticResource HeaderTextStyle}"

    Text="InStep Student's Overview" />

    <my:DataGrid x:Name="dataGrid1" AutoGenerateColumns="True"/>

    </Grid>

    </bureauBlack:BureauBlackTheme>

    </navigation:Page>

    Can anyone suggest an option?

     And my Student class is,

    public class Student

    { public String StudentName { get; set; }

    public int StudentId { get; set; }

    public decimal Project1Score { get; set; }

    public decimal Project2Score { get; set; }

    public decimal Project3Score { get; set; }

    }

    And my Overview.xaml.cs is,

    public

    partial class Overview: Page

    { public Overview()

      {  InitializeComponent();

          dataGrid1.ItemsSource = GetStudents();  }

         public ObservableCollection<Student> GetStudents() {

          var studentList = new ObservableCollection<Student>()

    { new Student(){StudentName="Frodo Baggins",Project1Score = 89M,Project2Score=93M,Project3Score=88M},

    new Student(){StudentName="Rosie Cotton",Project1Score = 97M,Project2Score=93M,Project3Score=94M},

    new Student(){StudentName="Samwise Gamgee",Project1Score = 83M,Project2Score=90M,Project3Score=85M},

    new Student(){StudentName="Peregrin Took",Project1Score = 69M,Project2Score=72M,Project3Score=75M}};

    return studentList; }

    Any suggestion?

    Thanks

    Manikandan

    Tuesday, March 13, 2012 5:44 AM

Answers

  • Hi Manikandan,

    DataGrid didn't show is because you give it such a tiny size.

    <Grid.ColumnDefinitions>

    <ColumnDefinition Width="*" />

    <ColumnDefinition Width="639*" />

    </Grid.ColumnDefinitions>

    Here you define two columns with width * and 639* which means the second column width is 639 times as the first one. And you didn't specify which column the DataGrid is added to, so DataGrid is added to the first column(default one). It makes the first column so small to display DataGrid.

    To solve this, you can add DataGrid to the second column or change the width defination of Grid Column.

     

    Best Regards,

    Wednesday, March 14, 2012 11:42 PM

All replies

  • Hi Manikandan,

    check below Silverlight DataGrid Sample

    http://www.codeproject.com/Articles/97609/Using-the-Silverlight-DataGrid-with-View-Model-MVV  

    Regards

    Vikky

    Tuesday, March 13, 2012 6:35 AM
  • Hi Manikandan,

    DataGrid didn't show is because you give it such a tiny size.

    <Grid.ColumnDefinitions>

    <ColumnDefinition Width="*" />

    <ColumnDefinition Width="639*" />

    </Grid.ColumnDefinitions>

    Here you define two columns with width * and 639* which means the second column width is 639 times as the first one. And you didn't specify which column the DataGrid is added to, so DataGrid is added to the first column(default one). It makes the first column so small to display DataGrid.

    To solve this, you can add DataGrid to the second column or change the width defination of Grid Column.

     

    Best Regards,

    Wednesday, March 14, 2012 11:42 PM
  • Hi Shi Ding,

    You are correct. I made a mistake there. But still the datagrid is not populated with the binded data!

    Can you please suggest me an option to get that?

    Thanks

    Manikandan

    Thursday, March 22, 2012 12:03 AM
  • Hi everyone,

    Please help me by clearing my doubts! Please!

    Thanks

    Manikandan

    Monday, March 26, 2012 12:42 AM
  • Manikandan,

    I just created a new Silverlight application with your code and it's working correctly!

    So I think than your application is not going to this page.

    Patrick

    The test application:

    MainPage.xaml

    <UserControl
      x:Class="SilverlightApplication1.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"             
      mc:Ignorable="d"
      d:DesignHeight="300" d:DesignWidth="400">
    
      <Grid x:Name="LayoutRoot">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="639*" />
        </Grid.ColumnDefinitions>
    
        <TextBlock x:Name="HeaderText"
                   Text="InStep Student's Overview" />
    
        <my:DataGrid x:Name="dataGrid1"
                     AutoGenerateColumns="True"
                     Grid.Column="2"/>
    
      </Grid>
    </UserControl>

    MainPage.xaml.cs

    using System;
    using System.Collections.ObjectModel;
    using System.Windows.Controls;
    
    namespace SilverlightApplication1
    {
      public partial class MainPage : UserControl
      {
        public MainPage()
        {
          InitializeComponent();
          dataGrid1.ItemsSource = GetStudents();
        }
    
    
        public ObservableCollection GetStudents() { var studentList = new ObservableCollection() { new Student(){StudentName="Frodo Baggins",Project1Score = 89M,Project2Score=93M,Project3Score=88M}, new Student(){StudentName="Rosie Cotton",Project1Score = 97M,Project2Score=93M,Project3Score=94M}, new Student(){StudentName="Samwise Gamgee",Project1Score = 83M,Project2Score=90M,Project3Score=85M}, new Student(){StudentName="Peregrin Took",Project1Score = 69M,Project2Score=72M,Project3Score=75M}}; return studentList; } } }

    Student.cs

    using System;
    
    namespace SilverlightApplication1
    {
      public class Student
      {
        public String StudentName { get; set; }
    
        public int StudentId { get; set; }
    
        public decimal Project1Score { get; set; }
    
        public decimal Project2Score { get; set; }
    
        public decimal Project3Score { get; set; }
    
      }
    }
    
    Monday, March 26, 2012 12:56 AM
  • Patrick,

    I didn't get what you are coming to say! Sorry!

    Did you mean that you can see the datagrid populated with the data?

    Thanks

    Manikandan

    Monday, March 26, 2012 2:21 AM
  • Manikandan,

    Did you mean that you can see the datagrid populated with the data?

    Yes, as you can see below:

    Populated data grid

    Patrick

    Monday, March 26, 2012 2:30 AM
  • Hi Manikandan,

    Your DataGrid is populated correctly on my side as well.

    Please add a breakpoint at dataGrid1.ItemsSource = GetStudents(); and check if GeStudents return correct data there.

     

    Best Regards,

    Tuesday, March 27, 2012 1:28 AM
  • Hi Shi Ding,

    Thanks for the reply!

    Even i tried the same thing. I got the ouput when a single variable from the list like this,

    label1.Content = tempList[0].StudentName;

    next to the dataGrid1.ItemsSource = GetStudents(); statement.

    In this case, i have a label below the grid. And i did get the value correctly.

    Any suggestions please!

    Thanks

    Manikandan

    Tuesday, March 27, 2012 1:50 AM
  • Manikandan,

    have you tried to write a small application just to test if it's working on your side?

    Which version of Silverlight are you using?

    Patrick

    Tuesday, March 27, 2012 1:57 AM
  • Hi Patrick,

    I am using SL3. I have tried using in a simple app. But the same results is obtained.

    Thanks

    Manikandan

    Tuesday, March 27, 2012 2:02 AM
  • Hi Manikandan,

    Could you please post all your current code here or upload your project onto skydrive, then we can test locally and try to find the cause of this issue.

     

    Best Regards,

    Tuesday, March 27, 2012 2:05 AM
  • Manikandan,

    I don't remember if there is a problem with SL3, but can you try with SL4, or even SL5?

    Patrick

    Tuesday, March 27, 2012 2:10 AM
  • Hi Shi Ding,

    My exact code is as follows:

    Overview.xaml.cs:

    namespace InStepSM.SL.Views

    {

    public partial class Overview: Page {

    public Overview()

    { InitializeComponent();

    ObservableCollection<Student> tempList = GetStudents();

    dataGrid1.ItemsSource = GetStudents();

    label1.Content = tempList[0].StudentName; }

    public ObservableCollection<Student> GetStudents() { var studentList = new ObservableCollection<Student>() {

    new Student(){StudentName="Frodo Baggins",Project1Score = 89M,Project2Score=93M,Project3Score=88M},

    new Student(){StudentName="Rosie Cotton",Project1Score = 97M,Project2Score=93M,Project3Score=94M},

    new Student(){StudentName="Samwise Gamgee",Project1Score = 83M,Project2Score=90M,Project3Score=85M},

    new Student(){StudentName="Peregrin Took",Project1Score = 69M,Project2Score=72M,Project3Score=75M}};

    return studentList; }

    protected override void OnNavigatedTo(NavigationEventArgs e){ }}}

    Overview.xaml:

    Same as the code present in the beginning of the thread.

    Thanks 

    Manikandan 

     

    Tuesday, March 27, 2012 8:13 AM
  • I don't do C# (I really like Embarcadero Prism better), but does having those "new Student" statements inside curly braces get the students added to the list?  Or do you need to do an Add method on the collection?

    ...

    Oh, never mind, I see you did a GetStudents into the TempList and that received at least 1.  So that is probably not your problem.

    I usually do all activity in the Loaded event handler rather than in the constructor.  MAYBE that is a problem.

    Tuesday, March 27, 2012 8:29 AM
  • It is really a good idea. thank you.

    Wednesday, March 28, 2012 11:47 PM
  • Hi Manikandan,

    I still cannot reproduce your issue on my side.

    Below is my runnable test application (SL3Test), please compare with yours and check to see if you miss anything.

    https://skydrive.live.com/redir.aspx?cid=08be6e47349eba40&resid=8BE6E47349EBA40!132&parid=8BE6E47349EBA40!106

     

    Best Regards,

    Friday, March 30, 2012 4:57 AM