【问题标题】:DataGridView listing selected item from ListBoxDataGridView 列出从 ListBox 中选择的项目
【发布时间】:2014-06-09 08:23:55
【问题描述】:

在我的表单 1 上有一个列表框,列出了数据库中的电影(绑定源)。

在我的表单 2 上,我放置了一个 datagridview,并希望在表单 1 的列表框中显示所选电影的数据。

BindingSource bindingSource1 = new BindingSource();
bindingSource1.DataSource = from s in db.Filmek where s.Filmcim == (string)f.lBfilmlista.SelectedValue select s;
dataGridView1.DataSource = bindingSource1;

我上面的代码的问题是,无论我是否从表单 1 的列表框中选择其他电影,它始终只列出第一部电影。我应该怎么做才能一直显示列表框中的选定项目?

【问题讨论】:

    标签: c# database linq data-binding datagridview


    【解决方案1】:

    将相同的代码放入列表框的选定索引更改事件中......

    因为这个数据源只会被调用一次(一开始)

    如果你每次绑定数据源时都使用列表框的 selectedIndexChange 事件,你会得到你需要的......

    一切顺利

    【讨论】:

    • 你能帮我怎么做吗?它们采用不同的形式。如果我从上面的代码中提取方法,它在 form1 ofc 中不起作用。我怎样才能使它兼容?
    【解决方案2】:

    在 Form1 中获取一个属性并将列表框选定的值分配给该属性 在列表选择的索引更改事件上打开 Form2

    在 Form2 加载使用你在 form1 中创建的 Created 属性

    例如你创建的属性:sValue

    那么你的查询会是这样的

    BindingSource bindingSource1 = new BindingSource();
    bindingSource1.DataSource = from s in db.Filmek where s.Filmcim == (string)form1.sValue select s;
    dataGridView1.DataSource = bindingSource1;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多