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?

  • one
    you update the data, but not their presentation, update the data - Specter
  • update dataGridView1.DataSource after adding new record - Yaroslav Schubert
  • just for example, if you use a DataTable for a DGV source and add a string to the DataTable, the grid will pick it up. I thought there would be something like that. I tried after adding the record to call again dataGridView1.DataSource = null; dataGridView1.DataSource = source. But new lines are not shown - Cheburashka


3 answers 3

since the community raised the question

DataGridView supports three types of datasors:

  • advanced, implementing IBindingListView , IBindingList - they support inserting new records from the DataGridView , and notify the DataGridView about inserting records into the datasource itself.
  • stupid, IList - just lists of objects. Of these, data is delayed in the DataGridView once, after which no changes are propagated to either side - there are no events for this in the IList interface.
  • indirect (not sure what to call more precisely) - IListSource . This is a datasource with one method - IListSource.GetList() , and depending on what it returns - IBindingList(View) or IList - DataGridView selects from the first two mechanisms.

DataTable / DataSet implements an IListSource with a return of IBindingListView , a wrapper class of DataView . Therefore, changes to the DataTable immediately displayed in the DataGridView . In addition, new rows from the DataGridView also fall into the DataTable .

In the case of the L2S, the call

 dataGridView1.DataSource = context.GetTable<Marka>(); 

returns just an IList . The DataGridView practically doesn’t get a “table”, but simply a list of objects at the time of installing the dataGridView1.DataSource - something that will return context.GetTable<Marka>().ToList() at that moment.

No notifications about inserting new rows will be received by the DataGridView . The only way to load changes into it is to manually give it a new datasource - once again execute dataGridView1.DataSource = context.GetTable<Marka>()

    after dataGridView1.DataSource = ... make dataGridView1.DataBind (); and you will be happy

    • DGV has no such method - Cheburashka
    • it is better to use System.Windows.Forms.BindingSource bind , SqlConnection, SqlClient (I do not remember exactly) specify DataSource = bind.Datasource; And accordingly, datagridview.datasource = bind.Datasource; The logic is this. - Vfvtnjd

    Try to do context.Refresh after context.SubmitChanges() , and not before. Now you are trying to read from the database before you write to it.