2011-06-16 61 views
3

IM通过数据集试图环和地方曾经有一个0我想改变这种状况为空值或空白到目前为止,我有这个如何通过数据集和更改数据的C#循环

foreach (DataRow drRow in ds.Tables[0].Rows) 
{    
    //loop through cells in that row 
     { 
     //if the value is 0 change to null or blank 
     } 
} 

能任何人都可以帮我解决这个问题吗?

+0

如果任务是取代所有的zeroe用'NULL'写出SQL UPDATE语句并执行脚本。将数据加载到'dataset'中的目的是什么?为什么你使用C#。 IMO SQL脚本是更好的解决方案 – 2011-06-16 12:28:26

回答

3

当然

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; 
       } 
      } 

     } 
    } 

我真的相信,没有人仍与数据集:(

+0

仍然有一些DataSet是有效选项的情况。 – thekip 2011-06-16 12:33:13

+0

只是例如? – Restuta 2011-06-16 12:34:26

+0

im使用数据集的唯一原因是我试图连接到Interbase数据库并操纵数据,然后在图表和网格视图中显示新数据。你能否提出一个更好的方法来做到这一点? – c11ada 2011-06-16 12:34:58

0

像这样的东西可能有帮助的工作原理:

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; 
} 
0

你可以做

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] = ""; 
        } 
       }