开发者

Building a DataTable in C# with one column at a time

I am trying to build a Retirement Calculator as a chance to make something useful and learn C# better. Currently, I am trying to build a DataTable with a dynamic amount of rows/columns.

For context, I ask the user for some inputs regarding salary, % of salary being invested, and expected ROI. I have some other stuff too, but it isn't really part of the issue I am having. Because the number of columns I need for the DataTable is unknown until the formula is run (while+for loop completes), I am having trouble doing things as DataColumns. Hopefully the code below will help.

I am really trying to build the table one column at a time. I realize I could reverse it to build it one row at a time because the Years before retirement (yearsRetire) is known, but I would prefer not to and I want to learn more. Sorry for the indentation and commenting. I tried to leave some of my commented coding attempts in there. Thanks for any help.

public double calcROI()
{     
   double testROI = 0.00;
   double tempRetireAmount = 0;
   double adjustRetire = goalAmount * (1 + (Math.Pow(inflation,yearsRetire)));

// Loop through ROI values until the calculated retire amount with the test ROI
// is greater than the target amount adjusted for inflation
while (tempRetireAmount < adjustRetire)
{
    //Increment ROI by 1% per while iteration
    testROI += .01;  

    //Make a new Column to hold the values for ROI for this while iteration
    //dtMain.Columns.Add(Convert.ToString(testROI));       
    //DataColumn tempdc = new DataColumn(Convert.ToString(testROI));

    //Loop through the number of years entered by user and see the amount
    //at Retirement with current ROI
    for (int i = 0开发者_运维百科; i < yearsRetire; i++)
    {
        //Main formula to calculate amount after i years
        tempRetireAmount = (tempRetireAmount + salary*savingsPct) * (1 + testROI);

        // Add value for this year/ROI to table/column
        //DataRow dr = .NewRow();
        //dr tempRetireAmount;
        //tempdc[i] = tempRetireAmount;

    }
    //Need to add column of data to my Main DataTable
    //dtMain.Rows.Add(dr);
    //dtMain.Columns.Add(tempdc);
}
return testROI;

}

UPDATE: Just as a note, I definitely am not saying it is the best approach as I am way out of my default area of knowledge. I would normally just build a matrix with max size (array[maxrows][maxcols])and fill it in. I was trying to think less C like and use some of the features of C#. If there is a much better way, I am happy to change the technique. Thanks.


I don't think it's a good idea to add columns to a DataTable after adding rows.

The best thing to do in this case is to compute everything into arrays and build the DataTable at the end


Well. I disagree with the approach taken here, as you explain in your question, however, for adding columns to a DataTable, you should take into account that the values for the currently added rows will be DBNull.

The DataColumn class, has two Properties that can be useful for you:

    // default value for the newly added rows
columna.DefaultValue = 0;

    // whether your column allows DBNull values (which is true for this case)
columna.AllowDBNull = true;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜