简体   繁体   English

插入并加载后如何刷新datagridview?

[英]How to refresh a datagridview after insert and on load?

I have a datagridview displayng a table of products, everything is working just fine, inserting, updating and deleting. 我有一个datagridview displayng产品表,一切工作正常,插入,更新和删除。 But the dgv only updates if I click on it. 但是dgv仅在我单击时更新。 How can I make it update on load and after I click the buttons of insert, delete and update? 单击加载,删除和更新按钮后,如何使其在加载时更新?

DGV enter method: 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;
    } 
}

Insert method: 插入方法:

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;
    }
}

从插入,删除和更新按钮单击事件中删除dataGridView1_Enter方法并调用ListaGrid方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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