[英]Select Data from combo box and shows the information to it in List Box c#
I encountered some problem whereby when the user selects the selected name inside the combo box, the data linked with the names selected will show out in the list box. 我遇到了一个问题,即当用户在组合框中选择所选名称时,与所选名称链接的数据将显示在列表框中。 I have problem making into this method.
我在将此方法制作时遇到问题。 The error falls here.
错误就在这里。
int selectLocStation = Convert.ToInt32(cbLocStation.SelectedValue);
Error: Unable to cast object of type'<>f_AnonymousType2'2 [System.String.System.Int32]'to type 'System.IConvertible'. 错误:无法将类型'<> 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;
}
}
This is what I do to store data inside the combo box. 这是我将数据存储在组合框中的操作。
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";
Any help will be greatly appreciated. 任何帮助将不胜感激。
private void Edit_TS_Load(object sender, EventArgs e)
{
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";
}
}
Answer is here! 答案在这里!
if to string is available I would try this 如果字符串可用,我会尝试这个
int selectLocStation = Convert.ToInt32(cbLocStation.SelectedValue.ToString());
or this 或这个
int selectLocStation =Int32.Parse(cbLocStation.SelectedValue.ToString());
you can also try this 你也可以试试这个
DataRowView drow = (DataRowView)cbLocStation.SelectedItem;
string str = drow.Row.ItemArray[0].ToString();
check if string has numbers and the convert it to int 检查字符串是否包含数字并将其转换为int
you can also add databind 您还可以添加数据绑定
BindingContext oBC = new BindingContext();
cbLocStation.BindingContext = oBC;
cbLocStation.DataBindings.Add(new Binding("SelectedValue", DeleteRT, "RequiredLocationStationID", false, DataSourceUpdateMode.OnPropertyChanged));
you are trying to convert the anonymous type from the query into timespan which is not possible since it doesn't implement the IConvertible. 您正在尝试将匿名类型从查询转换为时间跨度,这是不可能的,因为它没有实现IConvertible。 you can write a wrapper class and select that class in the query.
您可以编写包装器类,然后在查询中选择该类。
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 WrapperClass
{
Time = t.Time2
}).ToList();
public class WrapperClass
{
public DateTime Time { get; set; }
}
where time is a DateTime you have defined in the wrapper class 其中time是您在包装器类中定义的DateTime
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.