smtnkc
smtnkc

Reputation: 508

Converting a List into Datatable

Assume that I have a list like PL = { P1, 0, 10, P2, 5, 20 } and I need to convert it a datatable like

ProcessName  ArrivalTime  CpuTime
P1           0            10
P2           5            20

The number of process (row count) is dynamic. And I have tried sth like this:

protected DataTable CreateDataTable()
{
    int j = 0;
    List<string> PL = CreateProcessList();
    DataTable DT = new DataTable();
    for (int i = 0; i < PL.Count - 2; i += 3)
    {
        DataRow ProcessRow = DT.NewRow();
        DT.Rows[j][0] = PL[i].ToString();
        DT.Rows[j][1] = Convert.ToInt32(PL[i + 1]);
        DT.Rows[j][2] = Convert.ToInt32(PL[i + 2]);
        j++;
    }
    DT.Columns.Add("Header", typeof(string));
    DT.Columns[0].ColumnName = "ProcessName";
    DT.Columns[1].ColumnName = "ArrivalTime";
    DT.Columns[2].ColumnName = "CpuTime";
    return DT;
}

It does not work (says that there is no row at position 0). Thanks for any idea.

Working Code After Editions:

    protected DataTable CreateDataTable()
    {
        List<string> PL = CreateProcessList();
        DataTable DT = new DataTable();
        DT.Columns.Add("ProcessName", typeof(string));
        DT.Columns.Add("ArrivalTime", typeof(int));
        DT.Columns.Add("CpuTime", typeof(int));

        for (int i = 0; i < PL.Count - 2; i += 3)
        {
            DataRow ProcessRow = DT.NewRow();
            ProcessRow[0] = PL[i].ToString();
            ProcessRow[1] = Convert.ToInt32(PL[i + 1]);
            ProcessRow[2] = Convert.ToInt32(PL[i + 2]);
            DT.Rows.Add(ProcessRow);
        }

        return DT;
    }

Upvotes: 0

Views: 120

Answers (3)

Neil Smith
Neil Smith

Reputation: 2565

Something like this should work. Keep adding to curRow until the current iteration starts with 'P'. When it does start with 'P', add the currentRow to the data table and start a new row.

DataTable dataTable;
DataRow curRow;

... add columns to dataTable

for (var i = 0; i < PL.Count; i++) {
    if (PL[i].ToString().StartsWith("P")) {
        if (curRow != null)
            dataTable.Rows.Add(curRow);

        curRow = dataTable.NewRow();
    }

    ... add PL[i] to curRow
}

There's a few little issues with this but they can be fixed pretty easily. A check to make sure curRow is not null before adding to data table... that sorta thing.

Upvotes: 0

Heslacher
Heslacher

Reputation: 2167

To create a datatable in the way you have described, you need to follow a different way.

  1. Create a datatable object
  2. Add Columns to the datatable object by using the Add() method
  3. use the datatable objects NewRow() method to get a DataRow object with the same schema as your datatable
  4. populate the columns of this DataRow with the desired values
  5. Add this DataRow to the Rows collection of your datatable object by using the Add() method
  6. repeat step 3 to 6 until your list reaches the end.

Upvotes: 1

Servy
Servy

Reputation: 203802

On the second iteration of your for loop i is 3, so you're getting the 4th row of your table (at this point in time your table has 2 row). You're then getting the 4th, 5th, and 6th columns of that row (your table has 0 columns, as you haven't added any columns yet) to set their value. The corresponding index out of range errors should be telling you exactly what's wrong here.

Don't access the i-th row from the table. Just use ProcessRow to access the row; it's right there in a variable for you. Don't access the i-th column, access the 1st, 2nd, and 3rd (and add the columns before you try to populate them.

Upvotes: 0

Related Questions