C# 在 DataRow 中设置项目的值不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/204406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:55:25  来源:igfitidea点击:

Setting value of an item in a DataRow does not work

c#datarow

提问by Serhat Ozgel

I am trying to convert all DateTime values in a DataTable to strings. Here is the method I use:

我正在尝试将 DataTable 中的所有 DateTime 值转换为字符串。这是我使用的方法:

private static void ConvertDateTimesToStrings(DataTable dataTable)
{
    if (dataTable == null)
    {
        return;
    }

    for (int rowIndex = 0; rowIndex < dataTable.Rows.Count; rowIndex++ )
    {
        for (int i = 0; i < dataTable.Columns.Count; i++)
        {
            DateTime dateTime;
            try
            {
                dateTime = (DateTime)dataTable.Rows[rowIndex][i];
            }
            catch (InvalidCastException)
            {
                continue;
            }
            dataTable.Rows[rowIndex][i] = dateTime.ToString("dd.MM.yyyy hh:mm:ss");
        }
    }
}

After this line works:

这条线工作后:

dataTable.Rows[rowIndex][i] = dateTime.ToString("dd.MM.yyyy hh:mm:ss");

I check the value of dataTable.Rows[rowIndex][i] and see it is still a DateTime, not a string. Why does this happen and how can I solve this?

我检查了 dataTable.Rows[rowIndex][i] 的值,发现它仍然是一个 DateTime,而不是一个字符串。为什么会发生这种情况,我该如何解决?

Edit: I am trying to do this because I am fighting an api and unfortunately I do not have the choice of which component to use.

编辑:我正在尝试这样做,因为我正在与 api 作斗争,不幸的是我无法选择使用哪个组件。

采纳答案by StingyHyman

It may have determined that the column data type is date time and is reboxing the values to datetime when you save the value back in there.

它可能已经确定列数据类型是日期时间,并且在您将值保存回那里时将值重新装箱为日期时间。

Try this... Create a new column on the datatable as a TypeOf(String), and save the string value into that column. When all the values have been copied, drop the date time column.

试试这个... 在数据表上创建一个新列作为 TypeOf(String),并将字符串值保存到该列中。复制所有值后,删除日期时间列。

回答by Biri

This simply won't work, because you haven't changed the underlaying data type.

这根本行不通,因为您没有更改底层数据类型。

You have a DataTable, with a column which has data type DateTime.

您有一个 DataTable,其中有一列数据类型为 DateTime。

You can assign a String to it, but it will convert back to DateTime.

您可以为其分配一个字符串,但它会转换回 DateTime。

Why do you want to change it to a formatted string? Can't you format only when you need to display it, and handle as a DateTime until you have to display it?

为什么要将其更改为格式化字符串?不能只在需要显示时才格式化,并在必须显示之前将其作为 DateTime 处理吗?

Update:it is also better if you check the column's type before you try to convert, it can be much faster:

更新:如果您在尝试转换之前检查列的类型也更好,它可以更快:

if (dataTable.Columns[0].DataType == typeof(DateTime))
{
}

回答by Eduardo Campa?ó

It won't work beacuse the DataType of the column is still DateTime and it'll convert the string back to datetime. I'd suggest to format the date to string when generating your API message. If you still need to generate a string column for datetime values

它不会工作,因为列的 DataType 仍然是 DateTime,它会将字符串转换回日期时间。我建议在生成 API 消息时将日期格式化为字符串。如果您仍然需要为日期时间值生成字符串列

foreach (DataColumn column in dataTable.Columns) {
  if (column.DataType == typeof(DateTime)) {
    dataTable.Columns.Add(column.ColumnName + "_string", typeof(string));
  }
}

foreach (DataRow row in dataTable.Rows) {
   foreach (DataColumn column in dataTable.Columns) {
     if (column.DataType == typeof(DateTime)) {
       row[column.ColumnName + "_string"] = row[column.ColumnName].ToString("dd.MM.yyyy hh:mm:ss");
     }
   }
}

Then you can remove all DateTime columns or use a dataTable.Select() to get only the columns you need. PS: I didn't test the code, it's up to you.

然后您可以删除所有 DateTime 列或使用 dataTable.Select() 仅获取您需要的列。PS:我没有测试代码,这取决于你。