Wednesday, May 4, 2011

string format 2 decimal places

Sometimes, little things take longer than most big complicated humongous functions.

All I wanted to do was to create a datatable with formated 2 places of decimals and it wasnt working... for hours


private DataTable createDdlTable()
    {
        // Create a new DataTable.
        DataTable myDataTable = new DataTable("gpaTable");
        // Declare variables for DataColumn and DataRow objects.


        // Create new DataColumn, set DataType, ColumnName and add to DataTable.    
        DataColumn myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "hrsToShow";
        myDataColumn.Unique = true;
        // Add the Column to the DataColumnCollection.
        myDataTable.Columns.Add(myDataColumn);




        // Instantiate the DataSet variable.
        DataSet myDataSet = new DataSet();
        // Add the new DataTable to the DataSet.
        myDataSet.Tables.Add(myDataTable);


        // Create three new DataRow objects and add them to the DataTable
        for (double i = 0.00; i <=9.50 ; i=i+0.50)
        {
            DataRow myDataRow = myDataTable.NewRow();
            myDataRow["hrsToShow"] = i.ToString("0.00");
            myDataTable.Rows.Add(myDataRow);
        }
        return myDataTable;
    }

No comments: