gpt4 book ai didi

c# - 当在创建客户表单中添加客户时,如何更新 MainForm 中的 Datagridview?

转载 作者:行者123 更新时间:2023-11-29 15:30:44 24 4
gpt4 key购买 nike

我正在尝试在 MainForm 的 datagridview 中显示数据。我正在创建客户表单中添加客户。

private void createButton_Click(object sender, EventArgs e)
{
string timestamp = Info.createTimestamp();
string userName = Info.getCurrentUserName();
if (string.IsNullOrEmpty(countryTxtBx.Text) ||
string.IsNullOrEmpty(cityTxtBx.Text) ||
string.IsNullOrEmpty(addressTxtBx.Text) ||
string.IsNullOrEmpty(cityTxtBx.Text) ||
string.IsNullOrEmpty(zipTxtBx.Text) ||
string.IsNullOrEmpty(phoneTxtBx.Text) ||
string.IsNullOrEmpty(nameTxtBx.Text) ||
(yesButton.Checked == false && noButton.Checked == false))
{
MessageBox.Show("Please enter all fields");
}
else
{
int countryId = Info.createRecord(timestamp, userName, "country", $"'{countryTxtBx.Text}'");
int cityId = Info.createRecord(timestamp, userName, "city", $"'{cityTxtBx.Text}', '{countryId}'");
int addressId = Info.createRecord(timestamp, userName, "address", $"'{addressTxtBx.Text}', '', '{cityId}', '{zipTxtBx.Text}', '{phoneTxtBx.Text}'");
Info.createRecord(timestamp, userName, "customer", $"'{nameTxtBx.Text}', '{addressId}', '{(yesButton.Checked ? 1 : 0)}'");
Close();
}
}

我能够从 MySql 中提取数据,但是当我添加或删除客户时,MainForm 中的 datagridview 不会更新。当我在“创建客户表单”中添加客户时,是否可以更新主表单中的 datagridview?

public void DataFill()
{
// Open connection
MySqlConnection conn = new MySqlConnection(Info.conString);
conn.Open();

// Create new DataAdapter
string query = $"SELECT * FROM customer";
MySqlDataAdapter SDA = new MySqlDataAdapter(query, conn);
DataTable dt = new DataTable();
SDA.Fill(dt);
// Render data onto the screen
customerDGV.DataSource = dt;
}

最佳答案

每次添加或删除客户时,您都可以刷新 MainForm 的 DataGridView。这是实现这一目标的方法...

//event to open the create customer form
private void OpenCreateCustomerForm(){
//first, create an instance of the form
frmCreateCustomer frm = new frmCreateCustomer();
/*second, add the function DataFill() to the FormClosed event of the create customer form, so everytime you close it, the MainForms datagridview will get updated*/
frm.FormClosed += new FormClosedEventHandler((object s, FormClosedEventArgs f) => { DataFill();});
//finally, show the create customer form
frm.ShowDialog();
};

关于c# - 当在创建客户表单中添加客户时,如何更新 MainForm 中的 Datagridview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58752713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com