无法将“System.DateTime”类型的对象转换为“System.String”类型。C#

埃齐奥·法伦泽

我正在尝试显示日期并将其恢复到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()
});

有什么办法吗?谢谢!PS 我正在使用 LiveCharts 和 MySQL。

莫希特·施里瓦斯塔瓦

你可以这样试试

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

如果需要,您还可以使用自定义日期和时间格式字符串将格式应用于ToString(dd-MM-yyyy) 中的日期

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”的错误MYSQL NET CORE 3.1

无法将类型为“ WhereArrayIterator`1 [System.String]”的对象转换为类型为“ System.String []”的对象

无法在C#中将类型为'System.String'的对象转换为类型为'Oracle.DataAccess.Client.OracleParameter'

SSIS:无法将类型为“ System.Datetime”的对象转换为类型为“ System.Char []”

C#无法将类型为“ System.Double”的对象转换为类型为“ System.Single”

无法在日期时间文字上将类型为“ System.String”的对象转换为类型为“ System.DateTime”的对象

无法在C#中将类型为'System.DBNull'的对象转换为类型为'System.String'

System.InvalidCastException:'无法将类型为'System.String'的对象转换为类型为'Newtonsoft.Json.Linq.JToken'。”

无法将类型为“ AttachmentDetailsDto”的对象转换为类型为“ System.String”的对象

无法将类型为“ System.Byte []”的对象转换为类型为“ System.String”的对象

无法将类型为“ System.Int16”的对象转换为类型为“ System.String”的对象

无法将类型为“ System.DBNull”的对象转换为类型为“ System.Byte []”的对象

错误无法在返回时将类型'string'隐式转换为'System.DateTime'

无法将类型为“ System.TimeSpan”的对象转换为类型为“ System.IConvertible”的对象

无法将类型为“ System.Decimal”的对象转换为类型为“ System.String”的对象。创建EDIT视图后

无法将类型为“ System.Collections.DictionaryEntry”的对象转换为类型为“ System.String”的对象

无法将类型为“ System.Windows.Forms.FlowLayoutPanel”的对象转换为“ System.Windows.Forms.Button c#”

SSIS:无法将“System.Decimal”类型的对象转换为“System.Char[]”类型

C# 无法将“TCSCapstone.frmInventory”类型的对象转换为“System.IConvertible”类型

无法将“WhereSelectArrayIterator`2[System.String,System.Byte]”类型的对象转换为“System.Byte[]”类型。网络

无法将类型“System.Linq.IQueryable<System.DateTime>”隐式转换为“System.DateTime”

无法将“System.String”类型的对象转换为“FileItem”类型

无法将“system.dbnull”类型的对象转换为“system.byte”类型。C#

'无法将类型为'System.String'的对象转换为类型'System.Windows.Controls.ComboBoxItem

如何修复异常:无法将“System.DBNull”类型的对象转换为“System.Byte[]”类型

无法将“System.EventArgs”类型的对象转换为“System.Windows.Forms.FormClosingEventArgs”

C# .NET EF 迁移无法将“System.Double”类型的对象转换为“System.Single”类型

收到此错误:无法将“System.DateTime”类型的对象转换为“Oracle.ManagedDataAccess.Client.OracleParameter”

将 HierarchyId 转换为字符串 - 无法将“System.Byte []”类型的对象转换为“System.String”类型