尝试从MS Access数据库填充ListView

时间:2018-01-25 04:02:56

标签: c# listview oledb

我正在尝试创建一个返回列的所有数据的方法,以便可以在listview上使用数据来填充它。

    public static Array populatelistview()
{
    OleDbConnection myConnection = GetConnection();
    string query = "SELECT * FROM trainlines_"; 
    OleDbCommand command = new OleDbCommand(query, myConnection); 
    command.Connection = myConnection;

    DataSet trainlinedata = new DataSet();
    trainlinedata.Clear();
    OleDbDataAdapter datareader = new OleDbDataAdapter(command);
    datareader.Fill(trainlinedata);
    myConnection.Close();
    return trainlinedata;
}

2 个答案:

答案 0 :(得分:0)

从问题来看,您似乎可以从相关来源获取数据。我认为你需要的是将这个结果数组绑定到Listview控件。像下面的东西

ListView1.DataSource = trainlinedata.Tables["yourTableName"];
ListView1.DataBind();

答案 1 :(得分:0)

您应该返回DataSet输入Array。因为trainlinedata声明为DataSet

public static DataSet populatelistview()
{
    OleDbConnection myConnection = GetConnection();
    string query = "SELECT * FROM trainlines_"; 
    OleDbCommand command = new OleDbCommand(query, myConnection); 
    command.Connection = myConnection; 
    DataSet trainlinedata = new DataSet();
    OleDbDataAdapter datareader = new OleDbDataAdapter(command);
    datareader.Fill(trainlinedata);
    myConnection.Close();
    return trainlinedata;
}