andremara
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) ;
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"
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Happy to help.
ASKER
Andre