Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datatable - adding a cell

Status
Not open for further replies.

seanbo

Programmer
Jun 6, 2003
407
GB
i have a datable. i'm trying to populate it from file. i have the data for a single column, from the file, stored as a double[]. i have added a column like this...

DataColumn col = new DataColumn();
col.DataType = System.Type.GetType(type);
col.ColumnName = name;
col.AutoIncrement = false;
col.Caption = name;
col.ReadOnly = false;
col.Unique = true;
dt.Columns.Add(col)

...where 'name' is a string and dt is my datatable. the dataType is set to double. i now want to add the contents of my array but don't know how. this is as far as i am...

double[] da = getDoubleArray(data);
for(int i=0; i<da.Length; i++)
{
//this is where i would like to add a cell to my column with the correct data in it
}
}

what is the peice of code i am missing?
 
sorry, worked it out. once again i'm gonna answer my own thread - but it's always good to get information like this out there. i repleaced the rem line with...

try{
dt.Rows[name] = da;
}
catch(System.IndexOutOfRangeException){
DataRow row = dt.NewRow();
dt.Rows.Add(row);
dt.Rows[name] = da;
}

...though the exception should never actually be thrown in the context i'm working in. i also changed the unique property to false - it shouldn't have been true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top