views:

802

answers:

2

In Silverlight 3, I am tring to produce a basic, populated datagrid, but the datagrid is displaying 4 empty rows.

The code snippets for the page.xaml and page.xaml.cs are below.

I think the data is correctly contained in e.Results, because when I step through the codebehind, the Web service is returning the List correctly populated, e.Results shows a count of 4, and the datagrid displays four empty rows. (I have not yet figured out how to see what exactly is in e.Results)

Here is the relevant code in page.xaml.cs:

void Page_Loaded(object sender, RoutedEventArgs e)
{
  Service1Client service = new Service1Client();
  service.GetAccountsCompleted += new
   EventHandler<GetAccountsCompletedEventArgs>(service_GetAccountsCompleted );
  service.GetAccountsAsync();

}

void service_GetAccountsCompleted(object sender, GetAccountsCompletedEventArgs e)
{
  this.grdData.ItemsSource = e.Result;
}

Here is the xaml definition of the datagrid:

<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="BegSilver.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
  <data:DataGrid x:Name="grdData"  Margin="15" AutoGenerateColumns="False">

  <data:DataGrid.Columns>

    <data:DataGridTextColumn Binding="{Binding acpk}" Header="acpk"></data:DataGridTextColumn>
    <data:DataGridTextColumn Binding="{Binding acctnumber}" Header="acctnumber"></data:DataGridTextColumn>
    <data:DataGridTextColumn Binding="{Binding name}" Header="name"></data:DataGridTextColumn>
    <data:DataGridTextColumn Binding="{Binding type}" Header="type"></data:DataGridTextColumn>

  </data:DataGrid.Columns>

</data:DataGrid>
</Grid>
</UserControl>

Any help would be greatly appreciated, Mike Thomas

A: 

A guess here, but I would venture that rather than setting the itemssource property, you should be setting the DataContext property. ItemsSource is for collection based controls such as a ListBox etc, and is used to define what collection to use to populate the items.

My guess is that you populated an object (Account) from a WCF service
If so, set

this.grdData.DataContex= e.Result; in your GetAccountsCompleted handler

Good Post here sumarrizing the differences between ItemsSource and DataContext http://anyedotnet.blogspot.com/2008/11/datacontext-vs-itemssource.html

A: 

I had the exact same problem. It turned out that the Foreground text color and background colors of the cells were the same. Just change those and you should be good. There's some examples here: http://silverlight.net/forums/t/42372.aspx

Add a foreground color style to your datagrid to see the text. Something like this:

<UserControl.Resources>
    <Style x:Key="gridStyle" TargetType="Controls:DataGrid">
        <Setter Property="Foreground" Value="Black" />
    </Style>
</UserControl.Resources>


<Controls:DataGrid x:Name="grid" AutoGenerateColumns="True" Style="{StaticResource gridStyle}">

...

Mike Hall