【问题标题】:Data doesn't show at the datagrid when added in the list bounded to an object DataSouce添加到绑定到对象数据源的列表中时,数据不会显示在数据网格中
【发布时间】:2018-09-14 16:06:44
【问题描述】:

我有一个与 dataGridView 绑定的人员列表,但是当我添加新数据并存储到列表中时,它并没有显示它已添加到 dataGridView 中。

代码如下所示

private IList<Person> personList = new List<Person>();

private void AddPerson()
    {

        var count = personList.Count;
        var finId = 0;
        if (count != 0)
        {
            var valid = personList.LastOrDefault();
            if (valid != null)
            {
                finId = valid.Id;
            }
        }

        var person = new Person
        {
            Id = ++finId,
            FirstName = tbFirstName.Text,
            LastName = tbFirstName.Text,
            Email = tbEmail.Text,
            DateOfBirth = dtDateOfBirth.Value
        };

        personList.Add(person);
    }

private void PopulateGrid()
    {
        //dataGridView1.DataSource = personList;
        dataGridView1.Rows.Add(new Person
        {
            Id = 1,
            FirstName = "Randolf",
            LastName = "Segubre",
            Email = "rsegubre@gmail.com",
            DateOfBirth = DateTime.Parse("08/28/1991"),
        });
        personBindingSource.DataSource = personList;
    }

private void Form1_Load(object sender, EventArgs e)
    {
        PopulateGrid();
    }

private void btnAdd_Click(object sender, EventArgs e)
    {
        AddPerson();
        MessageBox.Show("Successfully Added","INFORMATION",MessageBoxButtons.OK, MessageBoxIcon.Information);

        this.Clear();

    }

dataGridView 显示的默认值,但是当您在程序运行时添加数据时,它不会显示 dataGridView。 但它显示它已添加到personList。只是没有在dataGridView中添加

【问题讨论】:

标签: c# winforms


【解决方案1】:

关于我得到的所有 cmets 和答案,谢谢你们。我找到了我需要的东西。这是我的答案。

private IList<Person> _personList = new List<Person>();

private void PopulateGrid()
    {
        BindingSource bs = new BindingSource();
        bs.DataSource = _personList;
        dataGridView1.DataSource = bs;
    }

private void AddPerson()
    {
        var count = _personList.Count;
        var finId = 0;
        if (count != 0)
        {
            var valid = _personList.LastOrDefault();
            if (valid != null)
            {
                finId = valid.Id;
            } 
        }

        var person = new Person
        {
            Id = ++finId,
            FirstName = tbFirstName.Text,
            LastName = tbFirstName.Text,
            Email = tbEmail.Text,
            DateOfBirth = dtDateOfBirth.Value
        };
        MessageBox.Show("Successfully Added", "INFORMATION", MessageBoxButtons.OK, MessageBoxIcon.Information);
        _personList.Add(person);
        this.PopulateGrid();
    }

我没有将 IList 更改为 BindingList。它保持不变,但我添加了一个 BindingSource。

我将列表添加到 bindingsource 的 DataSource 中,然后将其放入 datagridview 的 DataSource 中。然后将其调用到 AddPerson 方法中,这样每次执行 AddPerson 时,datagridview 也会重新加载。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 1970-01-01
    • 2012-06-13
    • 2016-01-20
    • 2021-04-25
    相关资源
    最近更新 更多