I Linq2Sql
, a very interesting thing. There was a small problem. There is a table DB. In the dataGridView
I display it. After adding a record to this table, it is necessary that the entity is updated, and accordingly the dataGridView
should display it. I made a Refresh
for the dataContext'a
but something does not come out - the added record is not displayed.
This is how the database table is described:
[Table(Name="Марка")] public class Marka : INotifyPropertyChanged { public Marka(string name) { MarkaName = name; } public Marka() { } int id; [Column(IsPrimaryKey = true, Name = "Код",DbType="Int",IsDbGenerated=true)] public int ID { get { return id; } set { id = value; } } string markaname; [Column(Name = "Наименование")] public string MarkaName { get { return markaname; } set { markaname = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("markaname")); } }public event PropertyChangedEventHandler PropertyChanged; }
This is how I add new entries from the form:
public partial class Form1 : Form { DataContext context; public Form1() { InitializeComponent(); context = new DataContext(@"D:\CSharp\!WPF\avtoSeller\avto.mdb"); dataGridView1.DataSource = context.GetTable<Marka>();//отображаю в гриде таблицу } private void button1_Click(object sender, EventArgs e)//добавляю новую запись { Marka m = new Marka("пример марки12"); context.GetTable(typeof(Marka)).InsertOnSubmit(m); context.Refresh(RefreshMode.KeepChanges); context.SubmitChanges(); }
The most interesting (at least for me), that after adding I deduce number of lines
Text = context.GetTable<Marka>().Count().ToString()
in the table, and it increases as it should be by 1, but why is the new entry not displayed in the grid?