我有一个datagridview
显示产品表,一切正常,插入,更新和删除。但dgv
仅在我点击它时才会更新。如何在加载时更新并在单击插入,删除和更新按钮后进行更新?
DGV输入方法:
// this is where i call the select method in the main form
private void dataGridView1_Enter(object sender, EventArgs e)
{
// sisDBADM is the class that holds all the sql querys
sisDBADM obj = new sisDBADM();
dataGridView1.DataSource = obj.ListaGrid();
}
public DataTable ListaGrid()
{
vsql = "SELECT NOME , PRECO FROM menu";
NpgsqlCommand objcmd = null;
if (this.conectar())
{
try
{
objcmd = new NpgsqlCommand(vsql, con);
NpgsqlDataAdapter adp = new NpgsqlDataAdapter(objcmd);
DataTable dt = new DataTable();
adp.Fill(dt);
return dt;
}
catch (NpgsqlException e)
{
throw e;
}
finally
{
this.desconectar();
}
}
else
{
return null;
}
}
插入方法:
public bool Insert(ArrayList p_arrInsert)
{
vsql = "INSERT INTO menu(nome,preco)" + "VALUES(@nome,@preco)";
NpgsqlCommand objcmd = null;
// conection try/catch adding the parameters
if (this.conectar())
{
objcmd = new NpgsqlCommand(vsql, con);
objcmd.Parameters.Add(new NpgsqlParameter("@nome", p_arrInsert[0]));
objcmd.Parameters.Add(new NpgsqlParameter("@preco", p_arrInsert[1]));
objcmd.ExecuteNonQuery();
return true;
}
else
{
return false;
}
}
答案 0 :(得分:1)
删除 dataGridView1_Enter 方法,并从插入,删除和更新按钮点击事件中调用 ListaGrid 方法。