Refresh ListView c #

2

Good morning. I have a Form1 with several methods. In one of the methods I create another form2. On Form1, I have a listView with values from the Database. In form2 I will insert new values. I wanted it in form1, I updated the ListView, but I can not. I already put this.refresh ();

Thank you very much

    
asked by anonymous 10.05.2017 / 12:33

2 answers

2

There are a number of ways to implement this situation, you could better describe your situation to help us best suit your case.

Considering that you will block form1 while form2 is open, and by "writing" on form2, it closes, returning to form1, can be done as follows:

1- In the event where form2 opens, use ShowDialog and if you return OK, you search the data again for the listView. In form2, at the end of the "Save" event report: "this.DialogResult = System.Windows.Forms.DialogResult.OK;"

Form1 code:

    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 Code:

    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 upgrade Form1, on Form1 declare the search method on the database as public, and pass the Form1 object as a parameter to Form2. In Write to Form2, run the search method on the database that is present on Form1. Code Sample:

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);
        }
    }
    
10.05.2017 / 13:47
1

I did it another way. I cleaned the ListView and then came back to fill it! So:

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

The search () is where I get the values to fill with the data for the ListView.

    
28.06.2017 / 10:41