【问题标题】:How to display a list inside another list in a Winfoms DataRepeater如何在 Winfoms DataRepeater 的另一个列表中显示一个列表
【发布时间】:2016-08-09 20:50:05
【问题描述】:

我正在使用 C# Windows 窗体和 codefirst 数据库 (Visual Studio 2013 Ultimate)。

是否可以在 Windows 窗体的另一个列表中显示一个列表? (重点是 - 显示 - 数据)。

请以本项目为例:https://postimg.cc/image/inunj8pxh/

我通常使用 powerpacks 的数据中继器显示一个列表。例如,当客户下订单时,我可以显示订单列表的 orderId、customerEmail、customerName 等。

但是,每个订单都包含许多不同的商品。到目前为止,我无法在显示父列表(订单)的数据中继器的每个元素内显示子列表(项目)的任何元素。每个item的外键是orderId,order的外键是item的列表(关系order...items是1..n)。

【问题讨论】:

  • 一张图片有助于了解您在寻找什么。另外:您对标签的选择似乎有些混乱。树视图包含嵌套列表。除此之外,您需要自己编写代码。这是一个编码accordion example
  • 我很快创建了一个新项目,因为我的真实项目包含敏感数据。请查看此图片以了解我的项目和可视化目标的完整概述。 postimg.org/image/inunj8pxh
  • 嗯,这看起来好像你应该使用嵌套的 flowlayoutpanels 和 usercontrols..你可能仍然想要使用手风琴示例中的激活/扩展机制,但它看起来并不紧行网格;所以似乎不需要 DGV。如果您需要它,请不要忘记在布局时提前考虑数据绑定。
  • 感谢您抽出宝贵的时间和您的意见 TaW!所以没有更简单的方法,例如使用datarepeater并添加一行代码将项目列表绑定到每个orderId的listBox1? (我只想显示它,没有别的。)同时,我将阅读嵌套的 flowlayoutpanels 和用户控件,因为我对这些主题一无所知。再次,非常感谢!
  • 我不知道数据中继器。我的印象是您希望嵌套列表显示 inside 外部列表的一个元素。如果这不是您的目标,那么您基本上有一个主/详细信息的情况,并且可以使用任何列表控件。 (ListBox 是个笨蛋,ListView 更灵活,如果您需要的布局不仅仅是一行文本..)

标签: c# winforms list code-first datarepeater


【解决方案1】:

我找到了解决方案!我花了一段时间才弄清楚如何控制数据中继器项目。阅读了许多其他论坛和教程,我逐渐完成了自己的工作。在屏幕截图中找到我的完整项目: http://i.stack.imgur.com/jFa7G.png

我们非常欢迎对代码进行任何改进。由于我对整个编程世界很陌生,我的代码可能没有优化,词汇的使用有时可能不准确。

在这里你可以找到我的 Form1 的代码:

命名空间 list_inside_list { 公共部分类Form1:表格 { 公共表格1() { 初始化组件(); } 私人无效Form1_Load(对象发送者,EventArgs e) { } protected override void OnLoad(EventArgs e) { //First we load the list of Orders to datarepeater1 Program.CompanyContext _context = new Program.CompanyContext(); List<list_inside_list.Program.Order> listOrders = _context.Order.ToList(); program_OrderBindingSource1.DataSource = listOrders; //I don´t know why, but we need to load the list of items as well, although we never use the listItems variable List<list_inside_list.Program.Item> listItems = _context.Item.ToList(); /* * 1. We will loop through all the datarepater1 items (which is fed with listOrders) * 2. We assign currItem as datarepeater1.CurrentItem in order to "select" the current item at index j, * although we will never user currItem * 3. We tell the program that of the current datarepeater item we want use the current Order object * 4. We go through each of the currentOrder.items and print the itemName * */ DataRepeaterItem currItem = new DataRepeaterItem(); for (int j = 0; j < this.dataRepeater1.ItemCount; j++) { this.dataRepeater1.CurrentItemIndex = j; currItem = dataRepeater1.CurrentItem; var currentOrder = (list_inside_list.Program.Order)program_OrderBindingSource1.Current; foreach (var item in currentOrder.items) { dataRepeater1.CurrentItem.Controls["richTextBox1"].Text = dataRepeater1.CurrentItem.Controls["richTextBox1"].Text + item.itemName + "\n"; } } } }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多