C# Dataset Dynamically Add DataColumn

cdatasetsql-server-2008wpf

I am trying to add a extra column to a dataset after a query has completed and populate that new column with data. I have a database relationship of the following:

    EmployeeGroups
     /        \    
 Groups       Employees

Empoyees holds all the data for that individual, I'll name the unique key the UserID.

Groups holds all the groups that a employee can be a part of, i.e. Super User, Admin, User; etc. I will name the unique key GroupID

EmployeeGroups holds all the associations of which groups each employee belongs too. (UserID | GroupID)

What I am trying to accomplish is after querying for all users I want to loop though each user and add what groups that user is a part of by adding a new column to the dataset named 'Groups' with a typeof string to insert the groups that user is a part of. Then by use of databinding populate a listview with all employees and their group associations

My code is as follows; Position 5 is the new column I am trying to add to the dataset.

string theQuery = "select UserID, FirstName, LastName, EmployeeID, Active from Employees";
DataSet theEmployeeSet = itsDatabase.runQuery(theQuery);
        DataColumn theCol = new DataColumn("Groups", typeof(string));
        theEmployeeSet.Tables[0].Columns.Add(theCol);
        foreach (DataRow theRow in theEmployeeSet.Tables[0].Rows)
        {
            theRow.ItemArray[5] = "1234";
        }

At the moment, the code will create the new column but when i assign the data to that column nothing will be assigned, what am I missing?

If there is any further explination or information I can provide, please let me know.

Thank you all

EDIT
Changed Database diagram, was wrong

Best Answer

when you use itemarray you must set all values at once.
so :

theRow.ItemArray = new object[] { x,y,z,s,d };

I agree it is not clear that you should use it that way..
I guess the getter makes a copy of the data and you fill element 5 of that copy

You should do :

theRow["Groups"] = "1234"

Or :

theRow[5] = "1234"
Related Topic