0

I currently have a DataSet which contains a single table.

Inside the single table there are 150,000 rows. Each row contains 15 columns.

What I need to do is trim the whitespace from all fields before and after (I would assume using String.Trim())

I have the following foreach loops which pull the data as required, however I'm unable to overwrite the values in the DataSet itself:

foreach (DataTable table in MyData.Tables)
{
    foreach (DataRow row in table.Rows)
    {
        foreach (object item in row.ItemArray)
        {
            // Trim Whitespace Here
        }
    }
}

Also, for one particular column I need to remove all whitespace (including the spaces in a string itself). Can this be done for a particular column using the above foreach?

4

1 回答 1

10
foreach (DataTable dt in ds.Tables)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    foreach (DataColumn col in dt.Columns)
                    {
                        if (col.ColumnName == "colName"))
                        {
                            dr[col] = dr[col].ToString().Replace(" ", "");
                        }
                        else if (col.DataType == typeof(System.String))
                        {
                            dr[col] = dr[col].ToString().Trim();
                        }
                    }
                }
            }
于 2013-07-16T11:28:49.940 回答