如何在 Winfoms DataRepeater 中的另一个列表中显示一个列表
How to display a list inside another list in a Winfoms DataRepeater
我正在使用 C# Windows Forms 和 codefirst 数据库(Visual Studio 2013 Ultimate)。
是否可以在 Windows 表单中显示另一个列表中的列表? (重点是-显示-数据)。
请以这个项目为例:https://postimg.cc/image/inunj8pxh/
我通常会显示一个包含 powerpacks 数据中继器的列表。例如,当客户下订单时,我可以显示订单列表的 orderId、customerEmail、customerName 等。
但是,每个订单都包含许多不同的项目。到目前为止,我无法在显示父列表(订单)的数据中继器的每个元素内显示子列表(项目)的任何元素。每个商品的外键是orderId,订单的外键是商品列表(关系order...items是1..n)。
我找到了解决办法!我花了一段时间才弄清楚如何控制数据中继器项目。阅读了许多其他论坛和教程,我逐渐完成了自己的工作。在截图中找到我的完整项目:
http://i.stack.imgur.com/jFa7G.png
非常欢迎对代码进行任何改进。由于我对整个编程世界都很陌生,我的代码可能没有得到优化,词汇的使用有时可能不准确。
在这里你可以找到我的 Form1 的代码:
namespace list_inside_list
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, 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";
}
}
}
}
}
我正在使用 C# Windows Forms 和 codefirst 数据库(Visual Studio 2013 Ultimate)。
是否可以在 Windows 表单中显示另一个列表中的列表? (重点是-显示-数据)。
请以这个项目为例:https://postimg.cc/image/inunj8pxh/
我通常会显示一个包含 powerpacks 数据中继器的列表。例如,当客户下订单时,我可以显示订单列表的 orderId、customerEmail、customerName 等。
但是,每个订单都包含许多不同的项目。到目前为止,我无法在显示父列表(订单)的数据中继器的每个元素内显示子列表(项目)的任何元素。每个商品的外键是orderId,订单的外键是商品列表(关系order...items是1..n)。
我找到了解决办法!我花了一段时间才弄清楚如何控制数据中继器项目。阅读了许多其他论坛和教程,我逐渐完成了自己的工作。在截图中找到我的完整项目: http://i.stack.imgur.com/jFa7G.png
非常欢迎对代码进行任何改进。由于我对整个编程世界都很陌生,我的代码可能没有得到优化,词汇的使用有时可能不准确。
在这里你可以找到我的 Form1 的代码:
namespace list_inside_list
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, 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";
}
}
}
}
}