ListView不会在C#Forms应用程序中刷新或更新

时间:2012-08-17 21:37:14

标签: c# forms listview refresh

我正在尝试在MS Visual Studios中编写一个简单的C#密码保护程序实用程序。我在formA中有一个listView,我想使用formB将新项添加到列表中。

FormB调用forceAdd,它是formA的成员。 forceAdd然后更新数据集并尝试通过调用populateList()来刷新listView。

问题是listView没有反映更改,直到我通过formA中的按钮或复选框调用populateList()函数。

例如,我可以在我的代码中调用populateList()50次无效,但只要我从formA上的按钮调用它就可以了。

(DieselPass = fromA,newForm = FormB,newForm :: Save调用forceAdd,formA :: new打开newForm)

(当我连接formA :: Edit ...按钮以填充并在保存后单击它时,listView更新)

我是新用户...此图片解释了: http://i50.tinypic.com/wk6bys.jpg

    public void forceAdd(String a, String u, String p)
    {
        accountsDataSet.tblAccounts.AddtblAccountsRow(a, u, p);
        tblAccountsTableAdapter.Update(accountsDataSet.tblAccounts);
        populateList();
    }

    private void populateList()
    {
        //make sure data set is up to date
        this.tblAccountsTableAdapter.Fill(this.accountsDataSet.tblAccounts);
        //clear existing items
        listView1.Items.Clear();
        for (int i = 0; i < accountsDataSet.tblAccounts.Count; i++)
        {
            String[] wtf;
            if (maskPasswords)
                wtf = new string[] { accountsDataSet.tblAccounts[i].Account, accountsDataSet.tblAccounts[i].Username };
            else
                wtf = new string[] { accountsDataSet.tblAccounts[i].Account, accountsDataSet.tblAccounts[i].Username, accountsDataSet.tblAccounts[i].Password };
            lvi = new ListViewItem(wtf);
            Console.WriteLine(lvi.ToString());
            listView1.Items.Add(lvi);
        }
    }

我已经尝试过的事情包括:

    listView1.Update();
    listView1.BeginUpdate();
    ...
    listView1.EndUpdate();
    listView1.Refresh();
    listView1.Clear();
    listView1.RedrawItems(0,i,t/f);
    this.Update();
    this.Refresh();

非常感谢任何帮助或提示,谢谢。

更多代码:

FormA在formB中设置引用:

    myNewForm.getForm = this;

FormB对formA的引用:

    private mainForm myForm = new mainForm();
    public mainForm getForm
    {
        get { return myForm; }
        set { myForm = value; }
    }        

FormB调用FormA的功能:

    myForm.forceAdd(accountBox.Text.ToString(), usernameBox.Text.ToString(), passwordBox.Text.ToString());

这是我需要做“调用/委托”的实例吗?

编辑2:

抱歉,我不够清楚,FormB调用FormA::populateList(),我将Console输出放在函数中以确认这一点,它成功地将项添加到数据集但只是不刷新列表。它就像知道这是一个不会让它刷新的外部形式。

我也把populateList设为public,没有工作

视觉工作室不会让pupulateList()来自(mainForm)this.Parent.,即使在加入myNewForm.ShowDialog(this);后,我是否必须包含某些内容或以其他方式传递该内容?

我对formA到myForm的引用有效,而不是更新列表..?

编辑3: 这也不起作用:

((mainForm)this.Parent).populateList();

1 个答案:

答案 0 :(得分:0)