Reputation: 4334
im trying to loop through a dataset and where ever there is a 0 i want to change that to a null value or blank so far i have this
foreach (DataRow drRow in ds.Tables[0].Rows)
{
//loop through cells in that row
{
//if the value is 0 change to null or blank
}
}
can any one help me out of this?
Upvotes: 3
Views: 18512
Reputation: 6637
You could do
DataTable dt = new DataTable();
.....
.....
DataRowCollection drColl = dt.Rows;
for(int j=0; j<drColl.Count;j++)
{
DataRow drRow = drColl[j];
object[] itemArr = null;
itemArr = drRow.ItemArray;
//loop through cells in that row
for(int i=0; i<itemArr.Length; i++)
{
//if the value is 0 change to null or blank
if (itemArr[i].ToString() == "0")
drRow[i] = "";
}
}
Upvotes: 0
Reputation: 45058
Something like this might help:
foreach (DataRow rows in table.Rows)
{
object value = null;
var cells = rows.ItemArray;
for (int i = 0; i < cells.Length; i++)
{
value = cells[i];
if (value != null && value.GetType() == typeof(int))
{
if ((int)value == 0)
{
cells[i] = null;
}
}
}
rows.ItemArray = cells;
}
Upvotes: 0
Reputation: 5903
Sure
foreach (DataRow drRow in ds.Tables[0].Rows)
{
for(int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
int rowValue;
if (int.TryParse(drRow[i].ToString(), out rowValue))
{
if (rowValue == 0)
{
drRow[i] = null;
}
}
}
}
I really believed that nobody still works with DataSets :(
Upvotes: 3