简体   繁体   English

C#如何将字符串添加到列表,以便可以从DataSource访问它们

[英]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.

相关问题 如何使用对象列表作为数据源向C#winforms项目添加gridview - How to add a gridview to a C# winforms project with a list of objects as the datasource 数据源已绑定到DataGridView时将字符串列表添加到DataGridView - c# - Add list of strings to the DataGridView when the DataSource is already bonded to DataGridView C#字典如何从带有单个键的重复值的分隔字符串列表中添加数据到字典 - c# dictionary how to add data from a delimited list of strings with repeated values for a single keyinto a dictionary 如何将消息框中的句柄信息值(或字符串)添加到 C# 的列表框中? - How can I add information values(or strings)of handle from Messagebox to List box in C#? 如何在 c# 中将一定数量的字符串从一个列表添加到另一个列表 - How to add certain number of strings from one list to another in c# 使用DataSource C#时在下拉列表中添加一个空值 - Add an empty value in dropdown list when using DataSource C# 如何将字符串列表从C#返回到托管C ++ - How to return List of strings from C# to Managed c++ 如何从C#中的字符串列表创建正则表达式? - how to create regex from list of strings in C#? 如何将来自数据源的数据添加到输入数据C#的同一行中? - How to add the data coming from the datasource in the same row of the input data C#? MS Charts C#DataSource from array or List - MS Charts C# DataSource from array or List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM