我必須自動重新加載包含品牌名稱串列的 gridview。當顯示“品牌已添加”訊息時,應該如何自動重新加載資料網格視圖。我有另一種形式的資料網格視圖。
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();
}
}
這是設計:

uj5u.com熱心網友回復:
如果您想在顯示“已添加品牌”訊息時重新加載 datagridview,您可以再次將資料讀取到 datagridview。
可以在包含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();
}
uj5u.com熱心網友回復:
希望根據您的代碼Brand是顯示的形式GridView。在這個表格中,您可以打開表格AddBrand。
var addBrand = new AddBrand();
var result = addBrand.ShowDialog();
if(result == DialogResult.Yes)
ReloadLogic()// you can read from db
如果您可以BtnSave_Click在 Brand表單中寫
this.DialogResult = DialogResult.Yes; Close();
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/433994.html
上一篇:當您沒有可呼叫的內容時,如何在STA執行緒上呼叫方法?
下一篇:C#保存檔案,使輸出為多行
