0

I've been working for this an hour but still I don't know what is the reason why it's not refreshing the listview after hitting the "NO" button in dialog box.

I have frmCompanyList (main form) and located here the listview next I have frmCompanyEntry (childform).

I already tried using this Refresh() and it's not working. But if I click the button "Refresh" in my frmCompanyList its working.

So here's my code:

frmCompanyEntry:

DialogResult dialogResult = MessageBox.Show(Global._strInsertAgainMsg + "company?", Global._strTitleMsg, MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
    clearAll();
}
else if (dialogResult == DialogResult.No)
{
    this.Close();
    clearAll();

    frmCompanyList _company = new frmCompanyList();
    _company.PerformRefresh();
}

Then here's the frmCompanyList:

public void PerformRefresh()
{
    __toolCmbStatus.SelectedIndex = 1;
    loadCompany(__toolTxtSearch.Text);
}

 public void loadCompany(string _strSearch)
 {
     try
     {
         ListViewItem Item;
         __lvwCompany.Items.Clear();

        string _strWhereStatement = "(fldCode LIKE '" + "%" + _strSearch + "%" + "' OR fldCompany LIKE '" + "%" + _strSearch + "%" + "' OR fldAddress LIKE '" + "%" + _strSearch + "%" + "' OR fldContactNo LIKE '" + "%" + _strSearch + "%" + "' OR fldContactPerson LIKE '" + "%" + _strSearch + "%" + "')";

        if (__toolCmbStatus.SelectedIndex.ToString() != "2") 
        { _strQry = "SELECT * FROM tblCompany WHERE " + _strWhereStatement + " AND fldActive = '" + __toolCmbStatus.SelectedIndex.ToString() + "' ORDER BY fldCompany ASC"; }
        else
        { _strQry = "SELECT * FROM tblCompany WHERE " + _strWhereStatement + " ORDER BY fldCompany ASC"; }

        using (SQLConnect.SqlCommandEx _SQLCMD = new SQLConnect.SqlCommandEx(_strQry))
        {
            DataTable dt = _SQLCMD.GetDataTable();
            __lblTotalRecord.Text = dt.Rows.Count.ToString();

            if (dt.Rows.Count == 0)
            {
                Item = new ListViewItem("");
                Item.SubItems.Add(Global._strEmptyMsg);
                Item.SubItems[0].ForeColor = System.Drawing.Color.Red;
                __lvwCompany.Items.Add(Item);
            }
            else
            {
                foreach (DataRow DR in dt.Rows)
                {
                    Item = new ListViewItem(DR[0].ToString());
                    Item.SubItems.Add(DR[2].ToString());
                    Item.SubItems.Add(DR[3].ToString());
                    Item.SubItems.Add(DR[4].ToString());
                    Item.SubItems.Add(DR[5].ToString());
                    Item.SubItems.Add(DR[1].ToString());

                    if (DR[6].ToString() == "False")
                    {
                        Item.SubItems.Add("Inactive");
                        Item.SubItems[6].ForeColor = System.Drawing.Color.Red;

                        for (int x = 0; x <= 6; x++)
                        {
                            Item.SubItems[x].Font = new Font(__lvwCompany.Font, FontStyle.Italic);
                        }
                        Item.UseItemStyleForSubItems = false;
                    }
                    else
                    {
                        Item.SubItems.Add("Active");
                        Item.SubItems[6].ForeColor = System.Drawing.Color.Green;
                        Item.UseItemStyleForSubItems = false;
                    }
                    __lvwCompany.Items.Add(Item);
                }
            }
        }
    }
    catch (Exception ex) { MessageBox.Show("Please contact your administrator. Error: " + ex, Global._strTitleMsg); }
}

1 Answer 1

1

The problem is with line.

frmCompanyList _company = new frmCompanyList();

you're instantiating form, calling method that loads data, but not actually showing the form. If you call _company.Show() after refresh you'll see your refreshed list (but with another instance of main form)

Instead of duplicate, you want to refresh data in already open form. In that case, you have to pass reference from your frmCompanyList (parent form) to child form (frmCompanyEntry). You can to that by constructor or by some property.

In your frmCompanyEntry form, add property

frmCompanyList _company = null;

modify your frmCompanyEnty constructor like this

public frmCompanyEntry(frmCompanyList parent)
{
    this._company = parent;
}

and when opening frmCompanyEntry, from parent from, modify that like this:

frmCompanyEntry _entry = new frmCompanyEntry(this);
_entry.ShowDialog();

This way you're passing reference to child form, so you can refresh it in your code. Your existing code should be then like this (notice that the line frmCompanyList _company = new frmCompanyList() is missing):

DialogResult dialogResult = MessageBox.Show(Global._strInsertAgainMsg + "company?", Global._strTitleMsg, MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
    clearAll();
}
else if (dialogResult == DialogResult.No)
{
    this.Close();
    clearAll();

    _company.PerformRefresh();
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the answer. but i have an error in "parent" saying cannot implicity convert type proj1.frmCompanyEntry to proj1.frmCompanyList. And also in frmCompanyEntry _entry = new frmCompanyEntry(this); saying the best overloaded method match has some invalid arguments.
oops, my bad. There is an error in constructor. I have edited an answer. It has to be like this public frmCompanyEntry(frmCompanyList parent)
wow! thanks a lot! I already give up on this. I've been working on this almost 4 hrs.. thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.