[英]c# how to add strings to a list so that they will be accessible from DataSource
i am doing this: 我正在这样做:
List<string> list = new List<string>();
while (myReader.Read())
{
reporttime = (string)myReader["reporttime"];
datapath = (string)myReader["datapath"];
list.Add(reporttime,datapath);
}
chart1.DataSource = list;
i am not exactly sure how i should be donig the list.Add
in order to be able to do the following: 我不确定我应该怎么做
list.Add
,以便能够执行以下操作:
chart1.Series["Series1"].XValueMember = "reporttime";
chart1.Series["Series1"].YValueMembers = "datapath";
question: how should i be adding the items to the list? 问题:我应该如何将项目添加到列表中?
It might work simply by doing 它可能只是通过
chart1.DataSource = myReader;
chart1.Series["Series1"].XValueMember = "reporttime";
chart1.Series["Series1"].YValueMembers = "datapath";
chart1.DataBind();
, as the documentation says the chart can bind from a data reader: ,正如文档所述,该图表可以与数据读取器绑定:
http://msdn.microsoft.com/en-us/library/system.web.ui.datavisualization.charting.chart.datasource.aspx http://msdn.microsoft.com/zh-CN/library/system.web.ui.datavisualization.charting.chart.datasource.aspx
The following is a list of objects that you can use as the data source:
以下是可用作数据源的对象列表:
DataView
资料检视
Data readers (SQL, OleDB)
数据读取器(SQL,OleDB)
DataSet
数据集
DataTable
数据表
[...]
[...]
There are other good answers, but since your comment asked how to use a DataTable instead I'll post this anyway. 还有其他很好的答案,但是由于您的评论提出了如何使用DataTable的建议,因此无论如何我都会发布它。 Besides, I don't see how those answers will let you specify the value members, because the code examples I've seen require a named item, and a list of strongs isn't going to do it.
此外,我看不到这些答案如何让您指定值成员,因为我所看到的代码示例需要一个命名项,而强项列表则无法做到这一点。 (See @Dan's post).
(请参阅@Dan的帖子)。
Instead of using a DataReader and using while(reader.Read())
而不是使用DataReader并使用
while(reader.Read())
use a DataAdapter to fill the datatable. 使用DataAdapter填充数据表。 (Assuming the DB is SQL Server, you'd use a SQLDataAdapter , but there are different DataAdapters.
(假设数据库是SQL Server,则可以使用SQLDataAdapter ,但是有不同的DataAdapter。
string connectionstring = "Some connection sting";
string sql = "Select reporttime, datapath from sometable";
System.Data.DataTable t = new System.Data.DataTable();
System.Data.SqlClient.SqlDataAdapter ad = new System.Data.SqlClient.SqlDataAdapter(sql, connectionstring);
ad.Fill(t);
chart1.DataSource = t;
chart1.Series["Series1"].XValueMember = "reporttime";
chart1.Series["Series1"].YValueMembers = "datapath";
List.Add is for one item only. List.Add仅适用于一项。 To add multiple items, you would want AddRange:
要添加多个项目,您需要AddRange:
list.AddRange(new[] { reporttime,datapath,finalconc,DBinstrument } );
However, if you're binding to this, it sounds like you want a list of objects, not strings...so something more like 但是,如果您要绑定到它,这听起来像您想要一个对象列表,而不是字符串...所以更像
List<object> list = new List<object>();
while (myReader.Read())
{
reporttime = (string)myReader["reporttime"];
datapath = (string)myReader["datapath"];
list.Add(new {
reporttime = (string)myReader["reporttime"],
datapath = (string)myReader["datapath"]
finalconc = "something",
DBinstrument = "somethingelse?"
});
}
chart1.DataSource = list;
Note, I would suggest in practice creating a business object for this instead of using an anonymous one. 注意,在实践中,我建议为此创建一个业务对象,而不要使用匿名对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.