Refresh Listview c#

Asked

Viewed 316 times

2

Good morning. I have a Form1 with several methods. In one of the methods I create another form2. In Form1, I have a listview with Database values. In form2 I will insert new values. I would like to have Listview updated in Form1, but I am not able to. I have already put this.refresh in.();

Thank you very much

  • 2

    Improve the question to have more chances of answer. https://pt.meta.stackoverflow.com/questions/1186/como-crea-um-exemplo-m%C3%Adnimo-completo-e-verific%C3%A1vel? Rq=1

2 answers

2


There are several ways to implement this situation, you could better describe your situation to help with the best way for your case.

Whereas you will block Form1 while form2 is open, and when "write" to form2, it closes, returning to Form1, it can be done as follows:

1- In the event where you open form2, use Showdialog and if you return OK, you search the data again for listview. In form2, at the end of the "Record" event enter: "this.Dialogresult = System.Windows.Forms.DialogResult.OK;"

Code of Form1:

    public Form1()
    {
        InitializeComponent();
    }

    public void AtualizaListView()
    {
        //Vai no banco de dados, e atualiza o listview

    }

    private void buttonInserir_Click(object sender, EventArgs e)
    {
        Form2 form = new Form2();
        if ( form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            AtualizaListView();
        }
    }

Form2:

    public Form2()
    {
        InitializeComponent();
    }

    private void buttonGravar_Click(object sender, EventArgs e)
    {
        try
        {
            //Grava no banco de dados

            //Se tudo estiver correto, 
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

Now, if you need to keep Form2 open, and still update Form1, in Form1 state the search method in the database as public, and pass the Form1 object as parameter to Form2. In Form2 Save, run the search method in the database present in Form1. Code Example:

Form1:

    public Form1()
    {
        InitializeComponent();
    }

    public void AtualizaListView()
    {
        //Vai no banco de dados, e atualiza o listview

    }

    private void buttonInserir_Click(object sender, EventArgs e)
    {
        Form2 form = new Form2();
        form.form1 = this;
        form.Show();
    }

Form2:

    public Form1 form1 { get; set; }
    public Form2()
    {
        InitializeComponent();
    }

    private void buttonGravar_Click(object sender, EventArgs e)
    {
        try
        {
            //Grava no banco de dados

            //Se tudo estiver correto, 

            form1.AtualizaListView();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
  • Very Obriagada @Rovann, helped a lot :)

  • dispose =] ... =]

1

I did it differently. I cleaned out Listview, and then I filled it back in! Thus :

listView.Items.Clear();
Search();

search() is where I get the values to fill in the data for Listview.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.