0

我遇到了一些问题,当用户在组合框中选择所选名称时,与所选名称链接的数据将显示在列表框中。我在使用这种方法时遇到问题。错误就在这里。

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";

任何帮助将不胜感激。

4

3 回答 3

1
  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";

        }
    }

答案就在这里!

于 2012-08-08T06:29:51.137 回答
0

如果字符串可用,我会试试这个

int selectLocStation = Convert.ToInt32(cbLocStation.SelectedValue.ToString());

或这个

int selectLocStation =Int32.Parse(cbLocStation.SelectedValue.ToString());

你也可以试试这个

DataRowView drow = (DataRowView)cbLocStation.SelectedItem;
string str = drow.Row.ItemArray[0].ToString();

检查字符串是否有数字并将其转换为 int

您还可以添加数据绑定

BindingContext oBC = new BindingContext();
    cbLocStation.BindingContext = oBC;
    cbLocStation.DataBindings.Add(new Binding("SelectedValue", DeleteRT, "RequiredLocationStationID", false, DataSourceUpdateMode.OnPropertyChanged));
于 2012-08-02T06:19:57.530 回答
0

您正在尝试将匿名类型从查询转换为时间跨度,这是不可能的,因为它没有实现 IConvertible。您可以编写一个包装类并在查询中选择该类。

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; }
        }

其中 time 是您在包装类中定义的 DateTime

于 2012-08-02T06:20:44.993 回答