2

I have a Dataset I get from my MySQL Databasen using a method from my WCF Service. This Dataset contains the list of the projects in my database. It's a list of the projects, displayed like this :

ID | Name

1 | Toast

(this is an example)

I want to add, in the end of the dataset, a new column, like this :

ID | Name | Status

1 | Toast | 1

I have another method that returns an int. For parameters, this method take the ID of a projet. For example, the id of Toast is 1. So I enter my method with the parameter 1. You see what I mean ?

So I have a dataset that contains this :

ID | Name

1 | Toast

and an int that contains this : 1

so, I want to add a new row, based on the ID of the project. Keep in mind I can have MANY others projects, so I need to check any row..

I don't know how to proceed... Thanks for your help.

Naeso
  • 21
  • 4
  • First add a new column to the DataTable and then add values to the new row. A DataSet consists of one or more DataTables. – jdweng Jan 14 '19 at 13:20

1 Answers1

0

Already answered here:

Add new column and data to datatable that already contains data - c#

DataTable dt = sql.ExecuteDataTable("sp_MyProc");    
dt.Columns.Add("Status", typeof(System.Int32));    
foreach(DataRow row in dt.Rows)
    row["Status"] = GetStatus(row["ID"]);   

A full example

void Main()
{   
    //setup 
    DataSet projects = new DataSet("ProjectsDataSet");
    projects.Tables.Add(new DataTable("ProjectsTable"));

    DataTable ProjectsDataTable = projects.Tables["ProjectsTable"]; 

    ProjectsDataTable.Columns.Add("id", typeof(Int32));
    ProjectsDataTable.Columns.Add("name", typeof(string));

    var row1 = ProjectsDataTable.NewRow();
    row1["id"] = 1;
    row1["name"] = "some project name";
    ProjectsDataTable.Rows.Add(row1);

    //you are here

    //add a column
    ProjectsDataTable.Columns.Add("status", typeof(Int32));

    //populate column
    foreach (DataRow row in ProjectsDataTable.Rows) {
        row["status"] = 123;        
    }

    projects.Dump(); // shows result, only works in linqpad 
}
CooncilWorker
  • 405
  • 3
  • 12