简体   繁体   中英

How to update the datagridview in user control using c#

I'm stuck on this project wherein it doesn't automatically update the content of my datagridview inside the user control .i have a form (frmCustomer)that contains the crud and user control that has datagridview.. i want to automatically update the usercontrol once i add something on form(frmCustomer). The usercontrol is attached on a panel that is in another form(frmMenu).. I've tried to call the methods in usercontrol thst updates the datagridview but it seems it doesn't work? This is my Code in UC_Customer where I fetch the data coming from the database .

UC_Customer

public void RetrieveCustomer()
        {
using (MySqlConnection sqlCon = new MySqlConnection(myConnectionString))
            {
                sqlCon.Open();
                MySqlDataAdapter sqlDa = new MySqlDataAdapter("Select * from tblcreateaccounts", sqlCon);
                DataTable dtbl = new DataTable();
                sqlDa.Fill(dtbl);

                dgridCustomer.DataSource = null;
                dgridCustomer.DataSource = dtbl;
                this.dgridCustomer.Columns["Sex"].Visible = false;
                this.dgridCustomer.Columns["Birthday"].Visible = false;
                this.dgridCustomer.Columns["Age_"].Visible = false;
            }
        }`

UC_Customer_Load()

  private void UC_Customer_Load(object sender, EventArgs e)
        {
            dgridCustomer.AutoGenerateColumns = false;
            setUpDataGrid();
            RetrieveCustomer();
            RemovingLag rev = new RemovingLag();
            rev.DoubleBuffered(dgridCustomer, true);
            age = DateTime.Today.Year - dtpBirthdate.Value.Year;

        }

FrmCreateAccount

String query = "Insert into tblcreateaccounts(CusFirstname,CusLastname,CusAddress,CusContact,CusGender,CusBirthday,CusAge) Values(@f,@l,@a,@c,@g,@b,@age)";
            using (MySqlConnection connection = new MySqlConnection(myConnectionString))
            using (MySqlCommand cmd = new MySqlCommand(query, connection))
            {
                cmd.CommandTimeout = 60;
                cmd.Parameters.AddWithValue("@f", txtfname.Text);
                cmd.Parameters.AddWithValue("@l", txtlname.Text);
                cmd.Parameters.AddWithValue("@a", txtadd.Text);
                cmd.Parameters.AddWithValue("@c", txtcontact.Text);
                cmd.Parameters.AddWithValue("@g", gender);
                cmd.Parameters.AddWithValue("@b", dtpBirthdate.Value.Date.ToString("yyyy-MM-dd"));
                cmd.Parameters.AddWithValue("@age", age);
                connection.Open();

                if (txtfname.Text.Equals("") || txtfname.Text.Equals("Firstname") ||
               (txtlname.Text.Equals("") || txtlname.Text.Equals("Lastname") ||
               (txtadd.Text.Equals("") || txtadd.Text.Equals("Address") ||
               (txtcontact.Text.Equals("") || txtcontact.Text.Equals("Contact")))))
                {
                    DialogResult dr = MetroMessageBox.Show(this, "Please fill all the fields Something", "Admin", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else if(rbtFemale.Checked==false && rbtMale.Checked==false)
                {
                    DialogResult dr = MetroMessageBox.Show(this, "Please Select Gender", "Admin", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else if(gender.Equals(0))
                {
                    DialogResult dr = MetroMessageBox.Show(this, "Please Select your Birthday", "Admin", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else 
                {

                    if (cmd.ExecuteNonQuery() == 1)
                    {
                        MessageBox.Show("Customer Information has Successfully added!", "Status Report", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Dispose();
                    }
                    else
                    {
                        MessageBox.Show("Customer Information hasn't successfully added", "Status Report", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    connection.Close();
                }
            }
        }

inside the user control, I have a button(Add Customer) that call the FrmCreateAccount via showDialog(), and when I successfully added a customer, it doesn't automatically update the datagridview in my control panel, it will just update once I click the button(refresh: I called the method RetrieveCustomer to update the datagrid) PS. This User control is embedded to Bunifu Page which is actually inserted in frmMenu as per seen in the image enter image description here

You want to update the other form when performing an action. If so, you can create a delegate on the "frmMenu" form and assign an action to it, which when adding something to "frmCustomer" executes the datagridview delegate action and updates. If you provide the code snippet we can help you better.

我已经解决了我的问题,我只是在每个用户控件(Singleton方法)中创建了一个实例,该实例允许我以对应的形式调用每个方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM