如何在windows DataRepeater中显示一个列表在另一个列表中
本文关键字:列表 另一个 一个 显示 windows DataRepeater | 更新日期: 2023-09-27 18:01:39
我使用c# Windows Forms和codefirst数据库(Visual Studio 2013 Ultimate)。
是否有可能在Windows窗体中显示另一个列表中的列表?(重点是显示数据)。
请以这个项目为例:https://postimg.cc/image/inunj8pxh/
我通常显示一个列表与电源包的数据显示。例如,当客户下订单时,我可以显示订单列表的orderId、customerEmail、customerName等。
然而,每个订单包含许多不同的项目。到目前为止,我还不能在显示父列表(订单)的datarepeater中的每个元素中显示子列表(项)的任何元素。每个项目的外键是orderId,订单的外键是项目列表(关系顺序…)项目是1..n)。
我找到解决方案了!我花了一段时间才弄清楚如何获得对数据副本项的控制。阅读了许多其他论坛和教程,我逐渐找到了自己的方法。在屏幕截图中找到我的完整项目:https://i.stack.imgur.com/jFa7G.png
我们非常欢迎对代码的任何改进。由于我对整个编程世界都是新手,我的代码可能没有经过优化,词汇表的使用有时可能不准确。
下面是我的Form1的代码:<>之前名称空间list_inside_list{公共部分类Form1: Form{公共Form1 (){InitializeComponent ();}private void 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";
}
}
}
}
之前}