【问题标题】:Modifying or replacing column values reading from the DataTable修改或替换从 DataTable 读取的列值
【发布时间】:2012-04-20 03:56:59
【问题描述】:

我正在尝试更新列中的值,例如从数据表中读取的以下代码块。目前,它只更新第一行的指定列值(字符串),但不遍历下一行并更新那些。我究竟做错了什么?请指教。

public void UpdateDescription(DataTable dataTable)
{
    if (dataTable != null && dataTable.Rows.Count > 0)
    {
        DataRow dr = dataTable.Rows[0];
        string dataDesc = string.Empty;
        int rowIndex = 0;
        dataDesc = dr["DataDesc"].ToString();

        if (rowIndex < dataTable.Rows.Count)
        {
            dr = dataTable.Rows[rowIndex];

            if (!dr.IsNull("DataDesc"))
            {
                if (dataDesc.Contains("STATE"))
                {
                    dataDesc = dataDesc.Replace("STATE", "").Trim();
                }

                if (dataDesc.Contains("HELLO ALL"))
                {
                    dataDesc = dataDesc.Replace("HELLO ALL", "").Trim();
                }

                if (dataDesc.Contains("("))
                {
                    dataDesc = dataDesc.Remove(dataDesc.IndexOf("(")).Trim();
                }
            }
            dr["DataDesc"] = dataDesc;
        }

        rowIndex++;

    }
}

【问题讨论】:

    标签: c# datatable


    【解决方案1】:

    你没有循环。试试这个:

    while (rowIndex < dataTable.Rows.Count) 
    { 
        dr = dataTable.Rows[rowIndex];
        ...
        dr["DataDesc"] = dataDesc;
        rowIndex++; 
    }
    

    或者如果你喜欢...

    for (int rowIndex = 0; rowIndex < dataTable.Rows.Count; ++rowIndex)
    ...
    

    另外,如果一行的“dataDesc”为空,它将在您的逻辑中获取前一行的值!

    【讨论】:

    • 那么,我怎样才能防止前一行的值呢? @n8wrl
    • rowIndex++ 总是转到下一行。 n8wrl 的回答是正确的,我投了赞成票。
    • 把 dr["DataDesc"] = dataDesc;在你的“如果不为空”中
    【解决方案2】:

    您需要循环数据表中的行。您只是在这一行中指定了一行,编号为 0:

    DataRow dr = journalTable.Rows[0];
    

    以下代码应该适合您:

        public void UpdateDescription(DataTable dataTable)
        {
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                foreach (DataRow dr in dataTable.Rows)
                {
                    String dataDesc = dr["DataDesc"].ToString(); 
    
                    if (!dr.IsNull("DataDesc"))
                    {
                        if (dataDesc.Contains("STATE"))
                        {
                            dataDesc = dataDesc.Replace("STATE", "").Trim();
                        }
    
                        if (dataDesc.Contains("HELLO ALL"))
                        {
                            dataDesc = dataDesc.Replace("HELLO ALL", "").Trim();
                        }
    
                        if (dataDesc.Contains("("))
                        {
                            dataDesc = dataDesc.Remove(dataDesc.IndexOf("(")).Trim();
                        }
                    }
                    dr["DataDesc"] = dataDesc;
    
                }
    
            }
        }
    

    【讨论】:

      【解决方案3】:

      您似乎只读取了string 值dataDesc 一次。

      也许您的意思是阅读每一行。我不确定。

      如果是这样,试试这个版本:

      public void UpdateDescription(DataTable dataTable) {
        if ((dataTable != null) && (0 < dataTable.Rows.Count)) {
          int rowIndex = 0;
          //DataRow dr = journalTable.Rows[0]; // What was this line for? "journalTable" is not defined here.
      
          if (rowIndex < dataTable.Rows.Count) {
            DataRow dr = dataTable.Rows[rowIndex];
      
            if (!dr.IsNull("DataDesc")) {
              string dataDesc = dr["DataDesc"].ToString();
              if (dataDesc.Contains("STATE")) {
                dataDesc = dataDesc.Replace("STATE", "").Trim();
              }
      
              if (dataDesc.Contains("HELLO ALL")) {
                dataDesc = dataDesc.Replace("HELLO ALL", "").Trim();
              }
      
              if (dataDesc.Contains("(")) {
                dataDesc = dataDesc.Remove(dataDesc.IndexOf("(")).Trim();
              }
              dr["DataDesc"] = dataDesc;
            }
          }
      
          rowIndex++;
      
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-12
        • 1970-01-01
        • 2017-04-20
        • 2017-10-05
        相关资源
        最近更新 更多