Link to home
Create AccountLog in
Avatar of andremara
andremaraFlag for Afghanistan

asked on

Assigning a value to a DataTable column

I want to sort a datatable by a combination of two columns, batch and seq. My strategy is
1. create a new column, track
2. concatenate each row's batch and seq value and assign to the new "track" column
3. sort by track
The problem is syntax error:
Cannot apply indexing with [] to an expression of type 'System.Data.DataTable'
I've tried a number of variations on that line.  Any ideas?  Thanks!

            private void InsertPMPT(DataTable dt)
            {
                    dt.Columns.Add( "track", typeof(string) );
                  //assigning batch+seq to all the rows' "track" field.
                  int icount = 0;
                  foreach(DataRow R in dt.Rows)
                  {
                                //PROBLEM HEREO!
                        dt[0].Rows[icount]["track"]=(String)R["batch"]+(String)R["seq"];
                        icount++;      
                  }
                  // Set PrimaryKey
                  //dt.Columns[ "track" ].Unique = true;
                  dt.PrimaryKey = new DataColumn[] { dt.Columns["track"] };
                  //now sort the dataset ascending by track column
                  string strExpr = "";
                  string strSort = "track";
                  DataRow[] foundRows;
                  // Use the Select method to find all rows matching the filter.
                  foundRows = dt.Select(strExpr,strSort);
ASKER CERTIFIED SOLUTION
Avatar of cheddar73
cheddar73

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of andremara

ASKER

Cheddar73, your first solution AND your brilliant little piece of code in your second solution ALSO worked.  I am truly grateful as your path has save my path several hours!  Thank you very much, dude :)
Andre
Avatar of cheddar73
cheddar73

Happy to help.