我遇到了一些問題,當用戶在組合框中選擇所選名稱時,與所選名稱鏈接的數據將顯示在列表框中。我有問題製作成這種方法。錯誤在這裏。從組合框中選擇數據並在列表框中顯示信息c#
int selectLocStation = Convert.ToInt32(cbLocStation.SelectedValue);
錯誤:無法投型的對象 '<> f_AnonymousType2'2 [System.String.System.Int32]' 爲類型 'System.IConvertible'。
private void cbLocStation_SelectedIndexChanged(object sender, EventArgs e)
{
using (satsEntities Setupctx = new satsEntities())
{
int selectLocStation = Convert.ToInt32(cbLocStation.SelectedValue);
var query = (from db in Setupctx.requiredtimings
join timing t in Setupctx.timings on db.RequiredTimingID equals t.TimingID
where db.RequiredLocationStationID == selectLocStation
select new
{
t.Time2
}).ToList();
List<TimeSpan> lstSelectedTime = new List<TimeSpan>();
foreach (var a in query)
{
lstSelectedTime.Add((TimeSpan)a.Time2);
}
lstTime.DataSource = lstSelectedTime;
}
}
這是我在存儲數據到組合框中。
using (satsEntities Setupctx = new satsEntities())
{
var DeleteRT = (from DelRT in Setupctx.requiredtimings
join locationstationname ls in Setupctx.locationstationnames on DelRT.RequiredLocationStationID equals ls.locationstationID
select new { ls.locStatname, DelRT.RequiredLocationStationID }).Distinct().ToList();
cbLocStation.DataSource = DeleteRT.ToList();
cbLocStation.DisplayMember = "locStatname";
cbLocStation.ValueMember = "RequiredLocationStationID";
任何幫助將不勝感激。
你如何初始化你的組合框?你能告訴我們你的組合框包含什麼值? – 2012-08-02 06:42:26
讓我告訴你我在組合框中存儲數據的代碼。 – rookie 2012-08-02 06:45:28