简体   繁体   English

如何将数据列值转换为小数以在图表中使用?

[英]How do I convert datacolumn values to decimals for use in a chart?

This is what I am trying to do: 这就是我想要做的:

chart1.Series["ser1"].Points.AddY(database1DataSet.DataTable1.MyColumn);

However: Series data points do not support values of type System.Data.DataColumn only values of these types can be used: Double, Decimal, Single, int, long, uint, ulong, String, DateTime, short, ushort. 但是:系列数据点不支持System.Data.DataColumn类型的值,只能使用以下类型的值:Double,Decimal,Single,int,long,uint,ulong,String,DateTime,short,ushort。

How do I convert to these types from a dataset. 如何从数据集中转换为这些类型。 Forgive me if this is obvious, just started learning. 如果这很明显,请原谅我,然后开始学习。

Well, if you have more than one row inside your DataTable, I guess a way would be this one: 好吧,如果您的数据表中有多个行,我想可能是这样的:

//loop through the rows of your DataTable 
foreach(DataRow row in database1DataSet.DataTable1)
{   //convert the value of each row before adding it
    chart1.Series["ser1"].Points.AddY(Convert.ToInt32(row["name of your column here"]));
}

In this case I converted the value into an Int32, but you can cast in any other you want, take a look at the documentation of the Convert class. 在这种情况下,我将值转换为Int32,但是您可以强制转换任何其他值,请查看Convert类的文档。

If you have a large datatable I would iterate through the rows as follows and add the points to your chart in the loop (ie there is no way to add the whole column as you have tried to do....each point has to be added seperately) 如果您有一个很大的数据表,我将按以下方式遍历各行,并将这些点添加到循环中的图表中(即,您无法像尝试那样添加整个列。...每个点都必须是分别添加)

        using (var reader = database1DataSet.DataTable1.CreateDataReader())
        {
            int colOrdinal = database1DataSet.DataTable1.Columns["YourColumnName"].Ordinal
            while (reader.Read())
            {
                chart1.Series["ser1"].Points.AddY(reader.GetInt32(colOrdinal));
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM