winforms 如何在datagridView中显示相关行?

axkjgtzd  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(90)

如何在DataGridView #2中显示相关行?通过激活用户在第一个表(dataGridView1)中选择的第一行来选择链接表中的数据。该集合包含数据,但datagridview2不显示它们。我希望得到一个数据集,但它没有显示。我错了。我使用NetCore7 WinForms我也试图使用绑定绑定,但结果是一样的下面我提出的数据模型和访问它从表单代码如何正确链接dataGridView1,datagridView2和我的数据模型?

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
    public List<Adress> Adr { get; set; } 
    public List<Work>? Work { get; set; } = null;
    public List<string> Telephonе { get; set; } 
    public List<string> Email { get; set; }
}
namespace Model
{
    public class Adress
    {
        public int Id { get; set; }
        public string City { get; set;}
        public string  Street{ get; set;}
        public string House { get; set;}

    }
}

public static class InitialData
{
    public static List<Person> ListPerson { get; set; } = new List<Person>();
    static InitialData()
    {
        ListPerson = new List<Person>()
        {
            new Person()
            {
                Id = 1,
                Name = "Ivan",
                LastName = "Drago",
                Email = new List<string>() { "ivan@xxx.com" },
                Telephonе = new List<string>() { "+0333" },
                Adr = new List<Adress>()
                {
                    new Adress()
                    {
                        Id = 1,
                        City = "NewYork",
                        Street = "Street01",
                        House = "33/3/4",

                    },
                    new Adress()
                    {
                        Id = 2,
                        City = "Detroit",
                        Street = "Street02",
                        House = "1/3"
                    }

                }
            },
            new Person()
            {
                Id = 2,
                Name = "Petr",
                LastName = "Habbo",
                Email =  new List<string>(){ "petr@vvv.com" },
                Telephonе = new List<string>() { "+888" },
                Adr = new List<Adress>()
                {
                    new Adress()
                    {
                        Id = 3,
                        City = "Chicago",
                        Street = "Street03",
                        House = "43",
                    }
                }
            }
        };

    }
}
public partial class MainForm : Form
{
    private void MainForm_Load(object sender, EventArgs e)
    {
        dataGridView1.DataSource = InitialData.ListPerson;
        dataGridView1.MultiSelect = false;
        dataGridView1.Rows[0].Selected = true;
    }

    private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
    {
        var g = (DataGridView)sender;
        if (g == null) return;
        if (g.SelectedRows.Count == 0 || g.SelectedRows.Count > 1)
            return;
        var row = g.SelectedRows[0];
        var Data2 = InitialData.ListPerson
            .Where(x => x.Id.Equals((int)row.Cells["Id"].Value))
            .Select(x =>new { x.Telephonе,x.Email }).ToList();
        dataGridView2.DataSource = Data2;

    }
}

字符串

vddsk6oq

vddsk6oq1#

问题是您绑定到包含两个列表的匿名类型{ x.Telephonе,x.Email }。一个这样的对象显示在一行中,但字段是不能在一个单元格中显示的列表。
电话列表和电子邮件列表可以具有不同的长度,并且必须在两个网格或列表框中单独显示。
我通过在第二个网格中显示所选人员的地址制作了一个工作示例。

public Form1()
{
    InitializeComponent();
    dataGridView1.DataSource = InitialData.ListPerson;
}

private void DataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
    var person = (Person)dataGridView1.Rows[e.RowIndex].DataBoundItem;
    dataGridView2.DataSource = person.Adr;

    // Assuming that we also have 2 ListBoxes
    listBox1.DataSource = person.Telephonе;
    listBox2.DataSource = person.Email;
}

字符串
请注意,List<string>DataGridView中显示不好,因为为绑定对象的每个属性创建了一个列,在字符串的情况下是Length属性。
如果希望在同一个GridView中显示电话和电子邮件,请创建一个类似

public class Contact
{
    public string Telephone { get;set; }
    public string EMail { get; set; }
}


并在Person类中添加一个属性

public List<Contact> Contacts { get; set; }


这是必需的,因为您不能在同一网格中显示两个不同的列表。
然后你可以绑定

dataGridView2.DataSource = person.Contacts;

相关问题