Blog Posts

Friday, April 23, 2010

Currency Masking in Silverlight Datagrid Cell


Introduction

The code shows how to implement currency masking (format like $21,900) in Silverlight DataGrid using the IValueConverter. User can edit the data in numbers with out entering the commas.

Background

Having knowledge in Silverlight data binding to DataGrid is enough to understand the code.

Using the code

Create a simple Silverlight application in Visual Studio and create a class implementing the IValueConverter in the Silverlight project created in the solution, to use the class as value converter to render the values to datagrid cells in the required format.
    ///
/// CurrencyConverter class converts the values to thousand seperated number in the grid.
/// 
public class CurrencyConverter : IValueConverter
{
/// 
/// Convert method will convert to string in a format disirable to show to user.
/// Value to format
/// Formatted string to bing to grid.
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int? inputInt = value as int?;
if (inputInt.HasValue)
{
return "$" + inputInt.Value.ToString("N0", culture);
}
else
{
decimal? inputDecimal = value as decimal?;
if (inputDecimal.HasValue)
{
return "$"+inputDecimal.Value.ToString("N0", culture);
}
}
return String.Empty;
}

/// 
/// ConvertBack method will convert back the entered text to value
/// value entered by user.
/// Target type to convert/// Current culture
/// Int value of the revenue
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string input = value as string;
if (input != null)
{
if (input[0] == '$')
{
input = input.Substring(1);
input = input.Replace(",", "").Replace(".", "");
Regex objRegNum = new Regex(@"^\d*[0-9](.\d*[0-9])?$");
if (!objRegNum.IsMatch(input))
input = "0";
return int.Parse(input);
}
else
{
input = "0";
}
}
return value;
}
}
Add the datagrid in your Silverlight page, that looks just like the following -


<UserControl x:Class="CurrencyMasking.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"

mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"

xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"

xmlns:local="clr-namespace:CurrencyMasking;assembly=CurrencyMasking">

<UserControl.Resources>

<local:CurrencyConverter x:Key="NumberFormatConverter"/>

</UserControl.Resources>



<Grid x:Name="LayoutRoot">

<data:DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left"

FontWeight="Bold" x:Name="grdEmp" Height="200"

RowBackground="#FF75A0D3" VerticalAlignment="Top"

CanUserResizeColumns="False">

<data:DataGrid.Columns>

<data:DataGridTextColumn Width="100" Header="Name"

Binding="{Binding Name}">

</data:DataGridTextColumn>

<data:DataGridTextColumn Width="100" Header="Salary"

Binding="{Binding Salary, Converter={StaticResource NumberFormatConverter}}">

</data:DataGridTextColumn>

</data:DataGrid.Columns>

</data:DataGrid>

</Grid>



</UserControl>



The 'NumberFormatConverter' resource is created from the class CurrencyConverter. The static resource is binded to the column to render the values in the Currency format.




Thursday, April 22, 2010

Chating in asp.net using Ajax

In this post I want to share my work on developing Chatting application that can be used in your regular ASP.Net applications. I am using AJAX and SQL server for backend. This can be used in ASP.Net 2.0 or more, as I am using the UpdatePanel. The application works as follows -
  • List of online users are displayed.
  • The current logged-in user will click on a selected to user name to open a new chat window, and types a message.
  • Once the message is sent, on the selected user's screen a new pop-up window will appear.
  • The users can communicate through the current windows.
  • A single user can communicate with multiple users at a time.


When the First user sends a message, the message is saved to backend for the selected user. When the application running on the recipient (implemented in default.aspx in sample) uses the Ajax to ping the server for messages for each 5 seconds.

function PingServer() {

PingServerForMsg();

serTimeout(PingServer, 5000);

}

.....

var requestURL = 'GetChatMsg.aspx';

xmlHttp = GetXmlHttpObject(stateChangeHanlder);

xmlHttp_Get(xmlHttp, url);

........

On response, process the response and show the message in pop-up if any messages found.

function stateChangeHandler() {

...

//if message found open chat window - see the attachment for the complete code

OpenChatBox(msg[0], msg[1]);

...

}


When a message is found at server a new pop-up appears on the recipient screen.

Visit http://www.codeproject.com/KB/aspnet/Chating_in_asp_net_Ajax.aspx to download source code.