Create second column in DataTable

3

I have DataTable where with the code:

DT.Column.Add("1ª", gettype(int32))  
    For I = 1 To 30  
        DT.Rows.Add(I)  
    Next(I)

This For enumerates column 1 from 1 to 30. 'DT is declared with a new DataTable , in this case the prog makes everything correct, but I would like to know if it is possible to add a second column without being by means of:

DT.Column.Add("1ª", gettype(int32))  
    For I = 1 To 30    
        DT.Rows.Add(I, I*2)    
    Next(I) 

This would be necessary because the formula in the second column varies according to the value of I in each row. Examples and / or help in C # are welcome.
Working with line I master relatively well, but column is always a headache for me.

I tried something like:

For I = 1 To 30  
    DT.Rows.Add(I)  
Next(I)  
DT.Column.Add("2ª", gettype(int32))
For I = 1 to 30
  DT.Rows.Add(I)
Next(I)

But without any success, it just creates column 2 and adds data to column 1.

    
asked by anonymous 11.11.2015 / 02:16

1 answer

2

If I understood correctly what you want, the solution is probably to do it this way:

DataTable DT = new DataTable();
DT.Columns.Add("1ª", typeof(int));
DT.Columns.Add("2ª", typeof(int));
for (int i = 0; i < 30; i++)
{
    var row = DT.NewRow();
    row["1ª"] = i;
    row["2ª"] = i;
    DT.Rows.Add(row);
}

Or this:

DataTable DT = new DataTable();
DT.Columns.Add("1ª", typeof(int));
DT.Columns.Add("2ª", typeof(int));
for (int i = 0; i < 30; i++)
{
    DT.Rows.Add(i, i);
}
    
11.11.2015 / 12:18