C# Dataset Dynamically Add DataColumn

Posted by Wesley on Stack Overflow See other posts from Stack Overflow or by Wesley
Published on 2010-05-14T23:56:57Z Indexed on 2010/05/15 0:04 UTC
Read the original article Hit count: 215

Filed under:
|
|
|

I am trying to add a extra column to a dataset after a query has completed. I have a database relationship of the following:

      Employees
     /        \    
 Groups       EmployeeGroups

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 a 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' which is a string to insert the values of the next query to get all the groups that user is a part of. Then by user 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

© Stack Overflow or respective owner

Related posts about c#

Related posts about dataset