单击按钮时只有两个列表框之一被填充
Only one of two listbox is being populated on the button click
我正在尝试使用从数据库中读取的两个不同列表来填充两个列表框。这是我的代码:
private void StartSchedule_Click(object sender, EventArgs e)
{
string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\A2 Computing\C# Programming Project\TriHard.accdb";
string SelectQuery = "SELECT Time.AthleteID, Athlete.AthleteName, Time.EventTime, Event.EventDistance FROM Event INNER JOIN (Athlete INNER JOIN [Time] ON Athlete.[AthleteID] = Time.[AthleteID]) ON Event.[EventID] = Time.[EventID];";
OleDbConnection Connection = new OleDbConnection(ConnectionString);
OleDbCommand Command = new OleDbCommand(SelectQuery, Connection);
Command.Connection.Open();
OleDbDataReader Reader = Command.ExecuteReader(CommandBehavior.CloseConnection);
PaceCalculator pace = new PaceCalculator();
List<PaceCalculator> Distancelist = new List<PaceCalculator>();
while (Reader.Read())
{
pace = new PaceCalculator();
pace.Distance = (int)Reader["EventDistance"];
Distancelist.Add(pace);
}
listBox1.DisplayMember = "Distance";
listBox1.DataSource = Distancelist;
List<PaceCalculator> TimeList = new List<PaceCalculator>();
while (Reader.Read())
{
pace = new PaceCalculator();
pace.Time = (string)Reader["EventTime"];
TimeList.Add(pace);
}
listBox2.DisplayMember = "Time";
listBox2.DataSource = TimeList;
}
当我按下按钮时,listBox1 被填充,但 listBox2 没有。我该如何解决这个问题,使两个框彼此相邻填充?
OleDbDataReader 提供了一种仅以转发模式从基础数据库中读取数据的方法。所以,当你到达循环的末尾时,你不能重新启动它希望从你的数据的 init 再次读取。
因此,只需从数据源中读取距离和时间值并将它们添加到单个 PaceCalculator 对象实例并将其添加到列表中即可。所有这些都在一个循环中。
退出循环时,只需将列表框的数据源设置为两个不同的列表并设置不同的 DisplayMember
属性
List<PaceCalculator> TimeList = new List<PaceCalculator>();
List<PaceCalculator> Distancelist = new List<PaceCalculator>();
while (Reader.Read())
{
pace = new PaceCalculator();
pace.Distance = (int)Reader["EventDistance"];
pace.Time = (string)Reader["EventTime"];
Distancelist.Add(pace);
TimeList.Add(pace);
}
listBox1.DisplayMember = "Distance";
listBox1.DataSource = Distancelist;
listBox2.DisplayMember = "Time";
listBox2.DataSource = TimeList;
我正在尝试使用从数据库中读取的两个不同列表来填充两个列表框。这是我的代码:
private void StartSchedule_Click(object sender, EventArgs e)
{
string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\A2 Computing\C# Programming Project\TriHard.accdb";
string SelectQuery = "SELECT Time.AthleteID, Athlete.AthleteName, Time.EventTime, Event.EventDistance FROM Event INNER JOIN (Athlete INNER JOIN [Time] ON Athlete.[AthleteID] = Time.[AthleteID]) ON Event.[EventID] = Time.[EventID];";
OleDbConnection Connection = new OleDbConnection(ConnectionString);
OleDbCommand Command = new OleDbCommand(SelectQuery, Connection);
Command.Connection.Open();
OleDbDataReader Reader = Command.ExecuteReader(CommandBehavior.CloseConnection);
PaceCalculator pace = new PaceCalculator();
List<PaceCalculator> Distancelist = new List<PaceCalculator>();
while (Reader.Read())
{
pace = new PaceCalculator();
pace.Distance = (int)Reader["EventDistance"];
Distancelist.Add(pace);
}
listBox1.DisplayMember = "Distance";
listBox1.DataSource = Distancelist;
List<PaceCalculator> TimeList = new List<PaceCalculator>();
while (Reader.Read())
{
pace = new PaceCalculator();
pace.Time = (string)Reader["EventTime"];
TimeList.Add(pace);
}
listBox2.DisplayMember = "Time";
listBox2.DataSource = TimeList;
}
当我按下按钮时,listBox1 被填充,但 listBox2 没有。我该如何解决这个问题,使两个框彼此相邻填充?
OleDbDataReader 提供了一种仅以转发模式从基础数据库中读取数据的方法。所以,当你到达循环的末尾时,你不能重新启动它希望从你的数据的 init 再次读取。
因此,只需从数据源中读取距离和时间值并将它们添加到单个 PaceCalculator 对象实例并将其添加到列表中即可。所有这些都在一个循环中。
退出循环时,只需将列表框的数据源设置为两个不同的列表并设置不同的 DisplayMember
属性
List<PaceCalculator> TimeList = new List<PaceCalculator>();
List<PaceCalculator> Distancelist = new List<PaceCalculator>();
while (Reader.Read())
{
pace = new PaceCalculator();
pace.Distance = (int)Reader["EventDistance"];
pace.Time = (string)Reader["EventTime"];
Distancelist.Add(pace);
TimeList.Add(pace);
}
listBox1.DisplayMember = "Distance";
listBox1.DataSource = Distancelist;
listBox2.DisplayMember = "Time";
listBox2.DataSource = TimeList;