修改或替换从DataTable读取的列值

时间:2012-04-06 14:44:10

标签: c# datatable

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

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

    }
}

3 个答案:

答案 0 :(得分: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++;

  }
}

答案 1 :(得分:1)

你没有循环。试试这个:

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

或者如果您愿意......

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

还有什么 - 如果“dataDesc”对于一行是空的,它将获得逻辑中前一行的值!

答案 2 :(得分: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;

            }


        }
    }