Обновление данных в dataGridView на той же форме

180
13 июля 2022, 19:50

Не получается обновить dataGridView после добавления данных в БД. Есть AssortmentForm на которой расположен dataGridView,а на дочерней форме добавляется запись в БД. И вернувшись на AssortmentForm после добавление записи, нужно что бы по нажатию кнопки "Обновить" обновилась dataGridView и отображалась с новой строкой. Пытался сделать различными способами, но ничего не выходило.

public partial class AssortmentForm : Form
{
    public AssortmentForm()
    {
        InitializeComponent();
        pictureBox1.Image = Image.FromFile($@"{Application.StartupPath}\FilmsImage\0.jpeg");
    }
    private void Assortment_Load(object sender, EventArgs e)
    {
        // TODO: данная строка кода позволяет загрузить данные в таблицу "cinemaDataBaseDataSet.Films". При необходимости она может быть перемещена или удалена.
        this.filmsTableAdapter.Fill(this.cinemaDataBaseDataSet.Films);
    }
    private void button2_Click(object sender, EventArgs e) //Кнопка обновления dataGridView
    {
        this.filmsBindingSource.EndEdit();
        this.filmsTableAdapter.Fill(cinemaDataBaseDataSet.Films);
        this.assortmentTable.Refresh();
    }

Код из дизайнера:

partial class AssortmentForm
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
        this.assortmentTable = new System.Windows.Forms.DataGridView();
        this.кодDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.названиеDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.описаниеDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.ценаDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.режиссёрDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.странаDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.filmsBindingSource = new System.Windows.Forms.BindingSource(this.components);
        this.cinemaDataBaseDataSet = new AIS_Kinoteatr.CinemaDataBaseDataSet();
        this.filmsTableAdapter = new AIS_Kinoteatr.CinemaDataBaseDataSetTableAdapters.FilmsTableAdapter();
        this.button1 = new System.Windows.Forms.Button();
        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        this.button2 = new System.Windows.Forms.Button();
        ((System.ComponentModel.ISupportInitialize)(this.assortmentTable)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.filmsBindingSource)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.cinemaDataBaseDataSet)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        this.SuspendLayout();
        // 
        // assortmentTable
        // 
        this.assortmentTable.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
        this.assortmentTable.AutoGenerateColumns = false;
        this.assortmentTable.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
        this.assortmentTable.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
        this.assortmentTable.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.кодDataGridViewTextBoxColumn,
        this.названиеDataGridViewTextBoxColumn,
        this.описаниеDataGridViewTextBoxColumn,
        this.ценаDataGridViewTextBoxColumn,
        this.режиссёрDataGridViewTextBoxColumn,
        this.странаDataGridViewTextBoxColumn});
        this.assortmentTable.DataSource = this.filmsBindingSource;
        dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
        dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Window;
        dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
        dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.ControlText;
        dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
        dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
        dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
        this.assortmentTable.DefaultCellStyle = dataGridViewCellStyle1;
        this.assortmentTable.Location = new System.Drawing.Point(232, 12);
        this.assortmentTable.Name = "assortmentTable";
        this.assortmentTable.RowHeadersVisible = false;
        this.assortmentTable.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.CellSelect;
        this.assortmentTable.Size = new System.Drawing.Size(802, 409);
        this.assortmentTable.StandardTab = true;
        this.assortmentTable.TabIndex = 0;
        this.assortmentTable.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.AssortmentTable_CellClick);
        // 
        // кодDataGridViewTextBoxColumn
        // 
        this.кодDataGridViewTextBoxColumn.DataPropertyName = "Код";
        this.кодDataGridViewTextBoxColumn.HeaderText = "Код";
        this.кодDataGridViewTextBoxColumn.Name = "кодDataGridViewTextBoxColumn";
        this.кодDataGridViewTextBoxColumn.Width = 51;
        // 
        // названиеDataGridViewTextBoxColumn
        // 
        this.названиеDataGridViewTextBoxColumn.DataPropertyName = "Название";
        this.названиеDataGridViewTextBoxColumn.HeaderText = "Название";
        this.названиеDataGridViewTextBoxColumn.Name = "названиеDataGridViewTextBoxColumn";
        this.названиеDataGridViewTextBoxColumn.Width = 82;
        // 
        // описаниеDataGridViewTextBoxColumn
        // 
        this.описаниеDataGridViewTextBoxColumn.DataPropertyName = "Описание";
        this.описаниеDataGridViewTextBoxColumn.HeaderText = "Описание";
        this.описаниеDataGridViewTextBoxColumn.Name = "описаниеDataGridViewTextBoxColumn";
        this.описаниеDataGridViewTextBoxColumn.Width = 82;
        // 
        // ценаDataGridViewTextBoxColumn
        // 
        this.ценаDataGridViewTextBoxColumn.DataPropertyName = "Цена";
        this.ценаDataGridViewTextBoxColumn.HeaderText = "Цена";
        this.ценаDataGridViewTextBoxColumn.Name = "ценаDataGridViewTextBoxColumn";
        this.ценаDataGridViewTextBoxColumn.Width = 58;
        // 
        // режиссёрDataGridViewTextBoxColumn
        // 
        this.режиссёрDataGridViewTextBoxColumn.DataPropertyName = "Режиссёр";
        this.режиссёрDataGridViewTextBoxColumn.HeaderText = "Режиссёр";
        this.режиссёрDataGridViewTextBoxColumn.Name = "режиссёрDataGridViewTextBoxColumn";
        this.режиссёрDataGridViewTextBoxColumn.Width = 83;
        // 
        // странаDataGridViewTextBoxColumn
        // 
        this.странаDataGridViewTextBoxColumn.DataPropertyName = "Страна";
        this.странаDataGridViewTextBoxColumn.HeaderText = "Страна";
        this.странаDataGridViewTextBoxColumn.Name = "странаDataGridViewTextBoxColumn";
        this.странаDataGridViewTextBoxColumn.Width = 68;
        // 
        // filmsBindingSource
        // 
        this.filmsBindingSource.DataMember = "Films";
        this.filmsBindingSource.DataSource = this.cinemaDataBaseDataSet;
        // 
        // cinemaDataBaseDataSet
        // 
        this.cinemaDataBaseDataSet.DataSetName = "CinemaDataBaseDataSet";
        this.cinemaDataBaseDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
        // 
        // filmsTableAdapter
        // 
        this.filmsTableAdapter.ClearBeforeFill = true;
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(12, 274);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(214, 29);
        this.button1.TabIndex = 2;
        this.button1.Text = "Добавить фильм";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // pictureBox1
        // 
        this.pictureBox1.Location = new System.Drawing.Point(12, 12);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(214, 256);
        this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
        this.pictureBox1.TabIndex = 1;
        this.pictureBox1.TabStop = false;
        // 
        // button2
        // 
        this.button2.Location = new System.Drawing.Point(12, 309);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(214, 29);
        this.button2.TabIndex = 3;
        this.button2.Text = "Обновить";
        this.button2.UseVisualStyleBackColor = true;
        this.button2.Click += new System.EventHandler(this.button2_Click);
        // 
        // AssortmentForm
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.AutoSize = true;
        this.ClientSize = new System.Drawing.Size(1046, 433);
        this.Controls.Add(this.button2);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.pictureBox1);
        this.Controls.Add(this.assortmentTable);
        this.MaximizeBox = false;
        this.Name = "AssortmentForm";
        this.Text = "Ассортимент";
        this.Load += new System.EventHandler(this.Assortment_Load);
        ((System.ComponentModel.ISupportInitialize)(this.assortmentTable)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.filmsBindingSource)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.cinemaDataBaseDataSet)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
        this.ResumeLayout(false);
    }
    #endregion
    private System.Windows.Forms.DataGridView assortmentTable;
    private System.Windows.Forms.BindingSource filmsBindingSource;
    private System.Windows.Forms.DataGridViewTextBoxColumn кодDataGridViewTextBoxColumn;
    private System.Windows.Forms.DataGridViewTextBoxColumn названиеDataGridViewTextBoxColumn;
    private System.Windows.Forms.DataGridViewTextBoxColumn описаниеDataGridViewTextBoxColumn;
    private System.Windows.Forms.DataGridViewTextBoxColumn ценаDataGridViewTextBoxColumn;
    private System.Windows.Forms.DataGridViewTextBoxColumn режиссёрDataGridViewTextBoxColumn;
    private System.Windows.Forms.DataGridViewTextBoxColumn странаDataGridViewTextBoxColumn;
    private System.Windows.Forms.PictureBox pictureBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    public CinemaDataBaseDataSetTableAdapters.FilmsTableAdapter filmsTableAdapter;
    public CinemaDataBaseDataSet cinemaDataBaseDataSet;
}
READ ALSO
Exe файл в качестве операционной системы

Exe файл в качестве операционной системы

Идея такая: есть исполняемый файл, написанный на C# (консольная программа)Он находится на флешке

246
Что я делаю не так? (корзина на сессиях)

Что я делаю не так? (корзина на сессиях)

Пытаюсь реализовать корзину в сессии, но по ходу мне нужна помощь

158