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

Tuesday, June 30, 2009

C#: Using comboBoxes in dataset/datatable bound dataGridViews

//Build fkey combobox 
 
var fKeyCombo = new DataGridViewComboBoxColumn
{
// most of these are frequently string values or constants 
HeaderText = "States", // The column header text 
DisplayMember = "StateAbbreviation",
// what field from the fkey table to display (list of names) 
Name = "state", // will be the name you use when you reference the column in the dataGridView 
ValueMember = "Id", // fkey link column typically the column in the foreign key table you join the main table on 
DataPropertyName = "stateId" // main table field you are replacing with a comboBox 
};
 
BindingSource fkey1Binding = new BindingSource();
var Fkey1Table = db.getDs().TechObjTypes;
 
fkey1Binding.DataSource = Fkey1Table; fKeyCombo.DataSource = fkey1Binding;
dataGridView1.Columns.Add(fKeyCombo);
 
//Add other fkey columns here or just delete below, and use the snippet over again  
//Sets up the main Table for dataGridView binding comment out or delete if using multiple fkey snippets //best declared with a larger scope than this function so it can later be submitted to the tableAdapter for updates
 
var mainTable = DataSet1.mainTable();
var MainBinding = new BindingSource();
MainBinding.DataSource = mainTable;
dataGridView1.DataSource = MainBinding;
//Must call MainBinding.EndEdit(); after updates, so it will update the MainTable 
// then you can pass the mainTable to a dataAdapter to do all the updates. 

Sunday, June 28, 2009

Regular Expressions :Regex.Replace()

I now have a coding blog, yay!

Today I learned how to match and replace at the same time in a Regex:

'remove leading = if it exists
lines(index) = Regex.Replace(lines(index), "(?\s*)=(?.+)", "${space}${therest}")
This makes use of named captures to remove a leading =, preserving opening whitespace.