0

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

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

    }
}
4

3 回答 3

2

看起来您只读取一次该stringdataDesc

也许您的意思是为每一行阅读它。我不知道。

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

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

  }
}
于 2012-04-06T14:54:28.207 回答
1

你没有循环。尝试这个:

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

或者,如果您愿意...

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

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

于 2012-04-06T14:47:28.107 回答
1

您需要循环数据表中的行。您只是在此行中指定一行,编号为 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;

            }

        }
    }
于 2012-04-06T14:48:32.650 回答