开发者

Add a datarow to a datatable based on the previous rows computation at runtime

I have the below datatable(kindly note that the columns numbers will not be known at compile time)

DataTable dt = new DataTable();
dt.Columns.Add("Summary");
dt.Columns.Add("Beta");
dt.Columns.Add("Delta");

dt.Rows.Add("Summary1", "n/a", "1");           
dt.Rows.Add("Summary2", "1.00", "2");

Now to this datatable , I have to add 1 more row that will be the subtraction of dt.Rows[0].Columns[i+1] - dt.Rows[1].Columns[i+1]

where i=0

so the final datatable w开发者_高级运维ill be

Summary   Beta  Delta   
---------------------------------
Summary1  n/a   1
Summary2  1.00  2
Summary3  n/a   -1

I am very new to dotnet. Please help


i'm not sure what you mean but by your final table i could think of this:

DataRow summary1 = dt.Rows[0], summary2 = dt.Rows[1], summary3 = new DataRow();
summary3[0] = "Summary3";

for(int i=1; i < summary1.Table.Columns.Count; i++)
{
  try{
    summary3[i] = double.Parse(summary1[i].ToString()) - double.Parse(summary2[i].ToString())
  }catch{
    summary3[i] = "n/a";
  }
}

This code allows you to have a variable amount of DataColumn inside the DataRow


I'm not sure what you want either, but I think you are having problems with the strings in the columns and converting them to ints and back. Here's an example of code that does the conversion:

    private string CalculateColumnTotal(int row, int column)
    {
        int column1Value;
        bool parsed = int.TryParse(_table.Rows[row][column].ToString(), out column1Value);
        if (!parsed) return "n/a";

        int column2Value;
        parsed = int.TryParse(_table.Rows[row + 1][column].ToString(), out column2Value);
        if (!parsed) return "n/a";

        var total = column1Value - column2Value;
        return total.ToString();
    }
}

The full form code looks something like this:

public partial class Form1 : Form
{
    private readonly DataTable _table = new DataTable("Table");

    public Form1()
    {
        InitializeComponent();
        _table.Columns.Add("Summary");
        _table.Columns.Add("Beta");
        _table.Columns.Add("Delta");

        const int rowPairs = 1;
        for (int i = 0; i <= rowPairs - 1; i++)
        {
            _table.Rows.Add("Summary1", "n/a", 1);
            _table.Rows.Add("Summary2", 1.00, 2);
            _table.Rows.Add("Summary3", null, null);
        }

        for (int row = 0; row < _table.Rows.Count - 1; row += 3)
        {
            string columnOneTotal = CalculateColumnTotal(row, 1);
            string columnTwoTotal = CalculateColumnTotal(row, 2);
            _table.Rows[row + 2][1] = columnOneTotal;
            _table.Rows[row + 2][2] = columnTwoTotal;
        }

        dataGridView1.DataSource = _table;
    }

    private string CalculateColumnTotal(int row, int column)
    {
        int column1Value;
        bool parsed = int.TryParse(_table.Rows[row][column].ToString(), out column1Value);
        if (!parsed) return "n/a";

        int column2Value;
        parsed = int.TryParse(_table.Rows[row + 1][column].ToString(), out column2Value);
        if (!parsed) return "n/a";

        var total = column1Value - column2Value;
        return total.ToString();
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜