Run method in Form1 for form2 change

Asked

Viewed 44 times

0

I’m developing an inventory manager, and the data is being stored in a database. The problem I’m facing is this: when I add a new product for example, I created a "Update list" button (I left it in red on the image), which when the user clicks on it, it updates the Listview and shows the new product inserted. inserir a descrição da imagem aqui

The following happens, when the person clicks on "New product" (to add a new product), opens a new form to register. What I am trying to do is to update the Listview of the products (which is in Form1), when the user registers a product by form2. I have already tried to instantiate the Form1 class and call the method, not any error, but the Listview of the products does not update. How can I resolve?

inserir a descrição da imagem aqui

Method to update the Listview I’m using:

// Método para atualizar a ListView Produtos
public void RefreshList()
{
    // Limpar o campo da List View
    listView_Cliente.Items.Clear();

    // Lógica para atualizar a list view
    con.Open();
    cmdListView = new SqlCommand("select * from produtos", con);
    da = new SqlDataAdapter(cmdListView);
    ds = new DataSet();
    da.Fill(ds, "estoque");

    con.Close();
    dt = ds.Tables["estoque"];

    int i;
    for (i = 0; i <= dt.Rows.Count - 1; i++)
    {
        listView_Cliente.Items.Add(dt.Rows[i].ItemArray[0].ToString());
        // temos 4 colunas (sendo uma a ID), então aqui só criamos 3, a ID vai automática
        listView_Cliente.Items[i].SubItems.Add(dt.Rows[i].ItemArray[1].ToString());
        listView_Cliente.Items[i].SubItems.Add(dt.Rows[i].ItemArray[2].ToString());
        listView_Cliente.Items[i].SubItems.Add(dt.Rows[i].ItemArray[3].ToString());
        listView_Cliente.Items[i].SubItems.Add(dt.Rows[i].ItemArray[4].ToString());

    }
}

Basically then I entered the class of Form1 (which is the method to update Listview) to form2, there I called the method to update Listview, Visual Studio did not present errors, however, List View does not update. I tried to do other ways in some gringas posts that I saw, however, also without success. I appreciate any help!

  • Already tried to call the update list method at the end of the registration method?

  • I tried, not from the bug either, but it doesn’t update. Do I have to make any changes to the code so that it identifies the Listview I want to update is in another form? I’m doing it this way: 1. I present the class that has the method and the List View: Telalogado updateTop = new Telalogado(); 2. I call the method on the instantiated object (within the sign-up method): updateStock.Refreshlist()

  • You don’t need to run the Bind() component after updating your items?

2 answers

0


Thanks to all the answers. What happened is that my specific case opened form2 and kept Form1 open. The only one who closed at the end was form2. Anyway, I was able to update the Listview by passing the parameter and opening the Form1 again (half Gambi), however, the Form1 was open in duplicity, and could not close the duplicity. As I was a long time stuck in this problem, I had to be objective in the solution (I saw on the NET that another way to solve this is through BIND, in case I need someone).

I decided as follows, when opening form2, instead of using only "form2.Show()", I used "form2.Showdialog()", and then called the method that updates my Listview. When you use Showdialog(), once I close form2, it calls this method and updates the List. It is not exactly what I was thinking, but it has already greatly improved the functioning of the program, and fulfilled the main objective that was to remove the button for the user to keep updating the list. Once I have more time I will study about BIND

How did the code turn out:

AddProduct telaProduto = new AddProduct();
            telaProduto.ShowDialog();
            RefreshList();

0

As a first step, instead of making new Form1(), at the time Voce makes new Form2(), it passes by parameter the listView_Cliente, so Voce will be passing by reference (when passing an object per parameter, in C# it is standard pass by reference) this component of Form1, so when you change something on it, it will reflect on the original instance of Form1. An interesting thing to understand in this case is that each instance has a 'life of its own', so if you create a new instance of Form1, it will not affect the previous instance of Form1. Understanding this is already a big step to understand the 'Runtime', the utility of having or not having new instances, and the importance of passing parameters by reference.

Once you get it right, there are more "elegant" ways to treat it, how to use delegates, it’s good to see and then understand even how some things work in windows Forms.

Browser other questions tagged

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