Wednesday, July 1, 2009

Silverlight dataGrid + grid issue

So it seems you can't use a dataset, datatable, datarow, or nearly anything else to send silverlight controls data. The only thing it did allow was LINQ to SQL.  Either way I finally got a data display going.
 Here's the Xaml.
[code]
1: <UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverlightApplication1.Page" 
2:  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
3:  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
4:  Width="500" Height="300"> 
5:  <!--If you don't put a Grid or some other container on your control you wind up only being able to display one 'thing'--> 
6:  <Grid x:Name="LayoutRoot" Background="White" ShowGridLines="True"> 
7: <!--If you don't give it row definitions it wound up only showing a single 'thing' in the grid--> 
8:  <Grid.RowDefinitions> 
9:  <RowDefinition Height="8*" /> <!--The star is for proportional sizing--> 
10:  <RowDefinition Height="2*" /> 
11:  </Grid.RowDefinitions> 
12:  
13:  <data:DataGrid x:Name="dgBLog" Grid.Row="0" /> <!-- for some reason it did not let me define a row and stick a dataGrid in it--> 
14:  <TextBox x:Name="lblStatus" Text="Starting" Grid.Row="1" /> <!-- used to show debug/status information --> 
15:   
16:  </Grid> 
17:  
18: </UserControl> 
[/code]
 
 
Then here's the Page.xaml.cs


 
namespace SilverlightApplication1
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
Loaded += new RoutedEventHandler(Page_Loaded);
}
 
void Page_Loaded(object sender, RoutedEventArgs e)
{
 
ServiceReference1.ServiceHomeqClient webService = new ServiceReference1.ServiceHomeqClient();
 
lblStatus.Text = "Loaded";
webService.BLogCountCompleted += new EventHandler<SilverlightApplication1.ServiceReference1.BLogCountCompletedEventArgs>(webService_BLogCountCompleted);
webService.BLogCountAsync();
webService.getErrorsCompleted += new EventHandler<SilverlightApplication1.ServiceReference1.getErrorsCompletedEventArgs>(webService_getErrorsCompleted);
webService.getErrorsAsync();
}
 
void webService_getErrorsCompleted(object sender, SilverlightApplication1.ServiceReference1.getErrorsCompletedEventArgs e)
{
this.dgBLog.ItemsSource = e.Result;
DisplayDiag();
}
 
 
 
void DisplayDiag()
{
var diag = new System.Text.StringBuilder();
diag.AppendLine("Finished retrieval");
 
this.lblStatus.Text = diag.ToString();
}
 
 
}
}
 
Here's the service code that feeds silverlight from a seperate asp.net project. It makes use of a Linq-to-sql class lqHomeQ.


using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
 
namespace SilverlightApplication1.Web
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ServiceHomeq
{
lqHomeQDataContext db = new lqHomeQDataContext();
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}
[OperationContract]
int BLogCount()
{
return db.bLogs.Count();
}
[OperationContract]
IEnumerable<bLog> getTodaysBLog()
{
//return db.bLogs.Where(row => row.dt.Date == DateTime.Today).ToList(); // table is larger than I'd like for this method
 
var result = from b in db.bLogs.Take(2)
select b;
return result.ToList();
}
[OperationContract]
IEnumerable<rejMtError> getErrors()
{
var result = db.rejMtErrors;
return result.ToList();
}
// Add more operations here and mark them with [OperationContract]
}
} 
 
  I based this off of this example
  from c-sharpcorner.com