![](/img/trans.png)
[英]c# access and update datagridview data from another user control
[英]How to update the datagridview in user control using c#
我被困在这个项目中,它不会自动更新用户控件中我的datagridview的内容。我有一个表单(frmCustomer),其中包含具有datagridview的crud和用户控件。我想自动更新usercontrol一旦我在form(frmCustomer)上添加了一些内容。 usercontrol附加在另一种形式的面板上(frmMenu)。我试图调用usercontrol中的方法以更新datagridview,但似乎不起作用? 这是我在UC_Customer中的代码,在这里我从数据库中获取数据。
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();
}
}
}
在用户控件中,我有一个按钮(添加客户),该按钮通过showDialog()调用FrmCreateAccount,当我成功添加客户时,它不会自动更新控制面板中的datagridview,只要单击该按钮,它就会更新按钮(刷新:我调用了RetrieveCustomer方法来更新数据网格)PS。 此用户控件嵌入到Bunifu页面中 ,该页面实际上已插入frmMenu中,如图像中所示,在此处输入图像描述
您要在执行操作时更新其他表单。 如果是这样,您可以在“ frmMenu”窗体上创建一个委托并为其分配一个操作,当向“ frmCustomer”添加某些内容时,该委托将执行datagridview委托操作并进行更新。 如果您提供代码段,我们可以为您提供更好的帮助。
我已经解决了我的问题,我只是在每个用户控件(Singleton方法)中创建了一个实例,该实例允许我以对应的形式调用每个方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.