0

I have to automatically reload the gridview which contains the list of the brand names. What should be done to automatically reload the data gridview when the "Brand has been added" meessage is shown.I have the data gridview in another form.

public partial class AddBrand : Form
{
    SqlConnection cn = new SqlConnection();
    SqlCommand cm = new SqlCommand();
    DBConnection dbcon = new DBConnection();
    public AddBrand()
    {
        InitializeComponent();
        cn = new SqlConnection(dbcon.MyConnection());
    }
    private void Clear()
    {
        btnSave.Enabled = true;
        btnUpdate.Enabled = false;
        tbBrand.Clear();
        tbBrand.Focus();
    }

    private void BtnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (MessageBox.Show("Do you want to save this brand?", " ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                cn.Open();
                cm = new SqlCommand("INSERT INTO tblBrand(brand)VALUES(@brand)", cn);
                cm.Parameters.AddWithValue("@brand", tbBrand.Text);
                cm.ExecuteNonQuery();
                cn.Close();
                MessageBox.Show("Brand has been added.");
                Clear();
            }             
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {

    }

    private void btnClose_Click(object sender, EventArgs e)
    {
        Brand brand = new Brand();
        brand.ShowDialog();
    }
}

This is the design:

enter image description here

Alp
  • 358
  • 5
  • 12
Sank2058
  • 1
  • 2
  • 1
    Does this answer your question? [Communicate between two windows forms in C#](https://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp) – JohnG Feb 18 '22 at 08:33
  • `SqlConnection cn = new SqlConnection();` don't hold onto SqlXxx things in form level variables. Make new ones every time you use them, and use `using` when you make them – Caius Jard Feb 18 '22 at 08:40
  • If you bind your DataGridView to a datatable that you share/add a record to/use an adapter to send changes back to the db then the grid will update automatically – Caius Jard Feb 18 '22 at 08:41

2 Answers2

0

Hope as per your code Brand is the form showing GridView. In this form you can open the form AddBrand.

var addBrand = new AddBrand(); 
var result = addBrand.ShowDialog();
if(result  == DialogResult.Yes)
ReloadLogic()// you can read from db 

In BtnSave_Click event in Brand form you can write

this.DialogResult = DialogResult.Yes; Close();
Bala
  • 149
  • 1
  • 7
0

If you want to reload the datagridview when the "Brand has been added" meessage is shown, you can read data to datagridview again.

You can refer to the following code in the form containing datagridview:

private void Form1_Load(object sender, System.EventArgs e)
    {
        SqlConnection cn = new SqlConnection();
        SqlCommand cm = new SqlCommand();
        DBConnection dbcon = new DBConnection();
        cn = new SqlConnection(dbcon.MyConnection());
        cn.open();
        string sql = "select * from tblBrand";
        SqlDataAdapter sda = new SqlDataAdapter(sql, cn);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        dataGridView1.DataSource = ds.Tables[0];
        cn.close();
    }
Jingmiao Xu-MSFT
  • 2,076
  • 1
  • 3
  • 10