c# - 无法将类型为 'System.DateTime' 的对象转换为类型 'System.String'。 C#

标签 c# mysql livecharts

我正在尝试显示日期并将其恢复到 Labels = strings, 但是我在行 list.Add((string)myReader["Date"]);

上遇到错误

这是我的代码:

con.Open();
myReader = cmdDB.ExecuteReader();
List<string> list = new List<string>();
while (myReader.Read())
{
    list.Add((string)myReader["Date"]);
}

string[] strings = list.Select(x => x.ToString()).ToArray();
cartesianChart1.AxisX.Add(new Axis
{
    Title = "Date",
    Labels = strings
});

cartesianChart1.AxisY.Add(new Axis
{
    Title = "Sales",
    LabelFormatter = value => value.ToString()
});

有什么解决办法吗?谢谢你!附言我正在使用 LiveCharts 和 MySQL。

最佳答案

你可以这样试试

list.Add(myReader["Date"].ToString());

如果需要,您还可以在 ToString(dd-MM-yyyy) using Custom Date and Time Format Strings 中对日期应用格式

关于c# - 无法将类型为 'System.DateTime' 的对象转换为类型 'System.String'。 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42524639/

相关文章:

php - 仅当所有字段与另一行不同时如何在数据库中插入行

c# - Livecharts Geared 在缩放到特定位置时抛出未处理的 ArgumentOutOfRangeException

c# - 使用 XAML 绑定(bind)设置具有自定义标签的 LiveCharts 系列的颜色

c# - 使用实时图表创建 WPF C# 图表

c# - Visual C# - 在哪里存储处理文件和工作而不是 C :\ProgramFiles?

类声明前的 C# XML 序列化属性

c# - 在 MVC 的每个页面上放置重复出现的代码的正确位置是什么

c# - 添加到 "open recent files"

php - SQL/PHP 为数据库表重新填充键 ID #s

mysql - 当表具有唯一值时查询显示重复项?