【问题标题】:Textboxes using BindingSource with more tables使用带有更多表格的 BindingSource 的文本框
【发布时间】:2014-04-20 22:45:57
【问题描述】:

在我的表单上有一个列表框,其中包含来自 table1 的电影标题,如果您从列表框中选择一个,您可以在来自 table1 的绑定来源的文本框中看到所有电影数据。

但是我有另一个表格,其中包含有关所选电影的 + 数据,但我不知道如何将文本框设置为该表格,因为如果我只是从 table2 中选择绑定源,它将无法以某种方式识别我需要的电影连接它们...

你能帮我解决一下吗?

这就是我添加 table1 的方式,所以当用户在文本框中输入时,它可以过滤列表框并选择电影

var h = from s in db.Filmek where s.Filmcim.StartsWith(textBox1.Text) select s;
            filmekBindingSource1.DataSource = h;

但我也需要 table2 中的连接数据显示在文本框中

【问题讨论】:

    标签: c# visual-studio-2010 binding textbox


    【解决方案1】:

    您是否尝试过在两个表之间进行连接?

    首先为两个表连接的结果创建一个类,类似于:

    public class MovieResult
    {
          public int MovieId {get;set;}
    
          public string MovieTitle {get;set;}
          //Other properties of table one
    
          //Properties of table two
    }
    

    然后将您的查询修改为如下所示:

    var result = from movie in db.Filmek 
    where movie.Filmcim.StartsWith(textBox1.Text) 
    join anotherTable in db.OtherTable on movie.FilmId equals anotherTable.FilmId
    selecte new MovieResult
    {
       MovieId = movie.Id,
       MovieTitle = movie.Title,
       //Fill the properties of table one
    
       //Fill the properties of table two, ex. if table two contains a field named Country
    
       Country = anotherTable.CountryName //etc
    }
    

    最后将其绑定到结果:

    filmekBindingSource1.DataSource = result;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      相关资源
      最近更新 更多