Wednesday, January 09, 2008

C# NET 2.0, Formatting and Validating Data

C# NET 2.0, Formatting and Validating Data


Binding CValueBinding = new Binding("Text", dsView, "contents.cvalue");
CValueBinding.Format += new ConvertEventHandler(this.textValue_FormatCurrency);
textCValue.DataBindings.Add(CValueBinding);

private void
textValue_FormatCurrency(object sender, ConvertEventArgs e)
{
// The method converts only to string type. Test this using the DesiredType.
if(e.DesiredType != typeof(string)) return;

// Use the ToString method to format the value as currency ("c").
e.Value = ((double) e.Value).ToString("c");
}

-------------


Binding notesBinding = new Binding("Text", dsView, "author.notes");
notesBinding.Format += new ConvertEventHandler(this.textBoxNotes_FormatBlob);
textBoxNotes.DataBindings.Add(notesBinding);
The following gives details of the Format Blob Routine

private void textBoxNotes_FormatBlob(object sender, ConvertEventArgs e)
{
Byte[] byteBLOBData
= new Byte[0];
byteBLOBData = (Byte[])e.Value;
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
e.Value = System.Text.Encoding.UTF8.GetString(byteBLOBData);
}


-----

dataGridView1.Columns["bal"].DefaultCellStyle.Format = "c";