将Windows表单中的DataGridView绑定到list< List< T>>. [英] Binding DataGridView in windows forms to a list<List<T>>

查看:420
本文介绍了将Windows表单中的DataGridView绑定到list< List< T>>.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义对象的集合,格式为T的List of List,即自定义对象的List of List. 我需要将此集合绑定到Windows窗体中的datagridview控件,并且页面数应等于外部列表中的内部列表数.每个页面应绑定到内部List,即T的List. 知道如何实现吗?

I have a collection of custom objects in format List of List of T , i.e, a List Of list of custom objects. I need to bind this collection to a datagridview control in windows forms, and the number of pages should be equal to the number of inner lists in the outer list. Each page should bind to inner List, that is, List of T. Any idea how this can be achieved ?

推荐答案

假定您的嵌套列表已被填充,并且除了DataGridView之外,您的表单还具有用于更改页面的PreviousNext按钮:您可以使用按钮更改索引,该索引指示将哪个嵌套列表用作DataSource.

Presuming that your nested list has been populated, and in addition to your DataGridView, your form has a Previous and Next button for changing pages: you could use the buttons to change an index which indicates which nested list is to be used as the DataSource.

public List<List<MyObject>> Pages { get; set; } // Populated elsewhere...
public int PageIndex { get; set; }

private void ChangePage()
{
  this.PreviousButton.Enabled = this.PageIndex > 0;
  this.NextButton.Enabled = this.PageIndex < this.Pages.Count - 1;
  this.dataGridView1.DataSource = this.Pages[this.PageIndex];
}

private void PreviousButton_Click(object sender, EventArgs e)
{
  this.PageIndex--;
  this.ChangePage();
}

private void NextButton_Click(object sender, EventArgs e)
{
  this.PageIndex++;
  this.ChangePage();
}

这篇关于将Windows表单中的DataGridView绑定到list&lt; List&lt; T&gt;&gt;.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆