gpt4 book ai didi

c# - 使用自定义对象的分层数据 (telerik RadGridView)

转载 作者:行者123 更新时间:2023-12-03 19:30:17 25 4
gpt4 key购买 nike

假设我有

class Person
{
public int Id {get;set;}
public string Name {get;set;}
public List<Person> All {get;set;}

public Person()
{
}

public List<Person> GetAll()
{
//fills the list with person and returns
}
}

我有:

class Address 
{
public int PersonId {get;set;}
public string theAddress {get;set;}
public List<Address> All {get;set;}

//constructor, etc

public List<Address> GetAll()
{
//fills the address list and returns
}
}

我想做的正是以下内容:

//filling the maintemplate with data
radGridView1.DataMember = "Person";
radGridView1.DataSource = new Person().GetAll();

//address template, the child one
GridViewTemplate template = new GridViewTemplate();
template.DataSource = new Address().GetAll();
template.DataMember = "Address";
radGridView1.MasterTemplate.Templates.Add(template);

//now the relation between those 2 classes

GridViewRelation relation = new GridViewRelation(radGridView1.MasterTemplate);
relation.ChildTemplate = template;
relation.RelationName = "PersonAddress"; //just a name
relation.ParentColumnNames.Add("Id"); //field to be "joined" to create the relation
relation.ChildColumnNames.Add("PersonId"); //same as above
radGridView1.Relations.Add(relation);

我得到的正是一个 GridView ,每个人旁边都有一个“+”号问题是,“子”网格是空的,如果我尝试添加数据(默认情况下,类中允许使用空构造函数),我会抛出 NullArgumentException

有什么想法吗?我几乎要放弃了。我的问题是:我在所有项目上使用自定义对象,它不像“你使用数据集,它准备好使用等”,我知道,但我想知道是否有办法使用自定义对象,或者我是否完成并且应该尝试数据集...

谢谢大家

最佳答案

看起来您正在使用 WinForms 实现。如果这是正确的,那么这对我来说很好。请尝试一下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;

namespace RadGridView_Hierarchy_CS
{
public partial class Form1 : Form
{

private List<Person> people = new List<Person>();
private List<Address> addresses = new List<Address>();

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
FillPeople();
FillAddresses();

radGridView1.DataSource = people;

GridViewTemplate template = new GridViewTemplate();
template.DataSource = addresses;
radGridView1.MasterTemplate.Templates.Add(template);

GridViewRelation relation = new GridViewRelation(radGridView1.MasterTemplate);
relation.ChildTemplate = template;
relation.RelationName = "PersonAddress";
relation.ParentColumnNames.Add("Id");
relation.ChildColumnNames.Add("PersonId");
radGridView1.Relations.Add(relation);

}

private void FillPeople()
{
Person richard = new Person();
richard.Name = "Richard";
richard.Id = 1;
people.Add(richard);
Person bob = new Person();
bob.Name = "Bob";
bob.Id = 2;
people.Add(richard);
Person mike = new Person();
mike.Name = "Mike";
mike.Id = 3;
people.Add(mike);
}

private void FillAddresses()
{
Address house1 = new Address();
house1.PersonId = 1;
house1.Id = 1;
house1.theAddress = "1 The Mews";
addresses.Add(house1);
Address house2 = new Address();
house2.PersonId = 2;
house2.Id = 2;
house2.theAddress = "2 The Mews";
addresses.Add(house2);
}
}

class Person
{
public int Id {get;set;}
public string Name {get;set;}


public Person()
{
}
}

class Address
{
public int Id { get; set; }
public int PersonId {get;set;}
public string theAddress {get;set;}

public Address()
{
}
}
}

关于c# - 使用自定义对象的分层数据 (telerik RadGridView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5024452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com