Friday, September 30, 2011

Converting and Formatting DataSet Strings

Consider the following. You have a DataSet and you need to assign the contest of this dataset to another string. In the example below, several TextBoxes.

You could do the below to convert the non-string data in the DataSet row to a String and then apply a String Format.

TextBoxDecimal.Text = ((decimal)lProjectsDS.Tables[0].Rows[0]["Budget"]).ToString("c");
TextBoxDouble.Text = ((double)lProjectsDS.Tables[0].Rows[0]["MaxHours"]).ToString("0.00");
TextBoxDate.Text = ((DateTime)lProjectsDS.Tables[0].Rows[0]["ExpDate"]).ToString("d");

The ToString format work as follows:
- Format: "c" converts to a currency using the default locate.
- Format: "0.00" converts the number so it has two decimals, if no decimals found then displays 00
- Format: "d" shortdate using the default locate.

You could also apply formatting to the TextBoxes or the target string, however in my case the need was to format the data you receive right out of the Dataset.

Happy Coding,
Will




No comments:

Post a Comment