如何以其他形式更新datagridview

时间:2016-08-17 21:00:44

标签: c# .net forms winforms datagridview

我有C#winforms app,我还有两种表单GroupsGroupAdd。在我以Groups格式插入数据后,我想在GroupsAdd表单内刷新/更新datagridview。

我谷歌和我发现了许多例子,但这些例子只适用于一种形式。

这是我的代码:

群组形式:

public partial class Groups : Form
{
   private void GrupeForm_Load(object sender, EventArgs e)
    {
        String query = "SELECT * FROM grups";

        using (MySqlConnection conn = new MySqlConnection(Properties.Settings.Default.connectionString))
        {
            try
            {
                using (MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn))
                {
                    adapter.SelectCommand = new MySqlCommand(query, conn);
                    DataTable table = new DataTable();
                    adapter.Fill(table);

                    BindingSource bSource = new BindingSource();
                    bSource.DataSource = table;

                    groupDataGridView.DataSource = bSource;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

GroupAdd表单:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace TrgoManager
{
    public partial class GroupAdd : Form
    {
        public GroupAdd()
        {
            InitializeComponent();
        }

        public static DialogResult Open()
        {
            GroupAdd self = new GroupAdd();

            DialogResult dialog = self.ShowDialog();

            return dialog;
        }

        private void otkaziBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void saveBtm_Click(object sender, EventArgs e)
        {
            try
            {
                using (MySqlConnection conn = new MySqlConnection(Properties.Settings.Default.connectionString))
                {
                    conn.Open();
                    string query = "INSERT INTO grupe (naziv, opis) VALUES(@naziv, @opis)";

                    using (MySqlCommand cmd = new MySqlCommand(query, conn))
                    {
                        try
                        {
                            cmd.Parameters.Add("@naziv", nazivGrupeTextBox.Text);
                            cmd.Parameters.Add("@opis", opisGrupeBox.Text);

                            cmd.ExecuteNonQuery();
                            MessageBox.Show("Uspesno ste kreirali novu grupu. Kliknite OK da biste zatvorili prozor", "Obavestenje", MessageBoxButtons.OK, MessageBoxIcon.Information);

                            this.Close();

                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Doslo je do greske prilikom kreiranje nove grupe. Greska = " + ex.Message);
                        }

                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

0 个答案:

没有答案