【问题标题】:SOLVED Datagridview Reload after Button click using Entity Framework?使用实体框架单击按钮后解决 Datagridview 重新加载?
【发布时间】:2021-08-10 08:50:43
【问题描述】:

我有一个 datagridview 并且每个 Button 我都可以添加项目,现在我想在 datagridview 中看到发生了一些变化。

我尝试了很多东西。 this.Refresh(); this.Invalidate(); 没什么用,

我找到了Application.Restart();,但我对这个解决方案不满意。 还有什么吗??

var contex = new frachtkostenEntities();
try
{
    var x = new Kunden()
    {
        Kundenname = first,
        Zielort = second,
    };

    contex.Kunden.Add(x);
    contex.SaveChanges();
}

现在新项目在数据库中,但 datagridview 保持不变

这是显示数据库的代码

frachtkostenEntities context = new frachtkostenEntities();
var item = from p in context.Artikel
            select new
            {
                Artikelnummer = p.Artikelnummer,
                Bezeichnung = p.Bezeichnung,
            };
dgvDisplayDataBase.DataSource = item.ToList();

【问题讨论】:

  • 欢迎您!如果您可以分享一些有用的代码。您甚至不必共享图片,但您可以复制/粘贴代码,然后在编辑器中将其格式化为代码。
  • var contex = new frachtkostenEntities();尝试 { var x = new Kunden() { Kundenname = first, Zielort = second, };
  • contex.Kunden.Add(x); contex.SaveChanges();

标签: c# winforms entity-framework datagridview


【解决方案1】:

你的问题是新数据还没有上传,你可以创建一个函数

void LoadData()
{
    //code load data from db
}

然后在事件AddButton中添加代码

void button1_Click(object sender, EventArgs e)
{
    //code add new record
    LoadData();
}

【讨论】:

    【解决方案2】:

    按照以下正确设置时,没有理由刷新、重新加载或无效。

    要在添加新项目时立即看到更改,请使用 BindingList 和 BindingSource 进行设置。然后将新项目添加到 BindingList。

    还可以考虑在模型中的属性值更改时使用 INotifyPropertyChanged。

    使用以下模型

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Newtonsoft.Json;
    using North.Interfaces;
    
    namespace North.Models
    {
        public partial class Contacts : INotifyPropertyChanged
        {
            private string _firstName;
            private string _lastName;
            private int? _contactTypeIdentifier;
    
            public int Id => ContactId;
            public int ContactId { get; set; }
    
            public string FirstName
            {
                get => _firstName;
                set
                {
                    _firstName = value;
                    OnPropertyChanged();
                }
            }
    
            public string LastName
            {
                get => _lastName;
                set
                {
                    _lastName = value;
                    OnPropertyChanged();
                }
            }
    
            public int? ContactTypeIdentifier
            {
                get => _contactTypeIdentifier;
                set
                {
                    _contactTypeIdentifier = value;
                    OnPropertyChanged();
                }
            }
    
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
            
        }
    }
    

    然后在表单中,加载数据到BindingList,添加按钮添加一条新记录并显示新的主键。

    namespace North.Forms
    {
        public partial class ContactAddForm : Form
        {
            
            private BindingList<Contacts> _bindingList;
            private readonly BindingSource _bindingSource = new BindingSource();
            
            public ContactAddForm()
            {
                InitializeComponent();
                
                dataGridView1.AutoGenerateColumns = false;
                Shown += OnShown;
            }
    
            private void OnShown(object sender, EventArgs e)
            {
                using (var context = new NorthwindContext())
                {
                    _bindingList = new BindingList<Contacts>(context.Contacts.ToList());
    
                    _bindingSource.DataSource = _bindingList;
                    dataGridView1.DataSource = _bindingSource;
                }
                
                _bindingSource.MoveLast();
    
            }
    
            private void AddNewContactButton_Click(object sender, EventArgs e)
            {
                // hard coded contact
                var newContact = new Contacts()
                {
                    FirstName = "Karen", 
                    LastName = "Payne", 
                    ContactTypeIdentifier = 1
                };
                
                // add to list and display in DataGridView
                _bindingList.Add(newContact);
    
                // save changes
                using (var context = new NorthwindContext())
                {
                    context.Add(newContact).State = EntityState.Added;
                    context.SaveChanges();
                }
    
                // See new primary key
                MessageBox.Show("Id " + newContact.ContactId.ToString());
            }
        }
    }
    

    编辑

    在 DataGridView 中查看当前行的值

    private void CurrentContactButton_Click(object sender, EventArgs e)
    {
        Contacts current = _bindingList[_bindingSource.Position];
        MessageBox.Show($"{current.ContactId}, {current.FirstName}, {current.LastName}");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多