Listview花费太长时间来更新C#

时间:2014-05-28 15:01:09

标签: c# sql database multithreading listview

我有一个问题,我已经坚持多年了,请你帮忙。

我有一个listview,在程序第一次加载时会填充我数据库中的数据。

在我的程序中,我有一个按钮,通过擦除列表视图中的所有数据并运行在程序加载时填充列表视图的相同方法来“刷新”此列表视图。但是,此刷新按钮需要太长时间(大约10秒)才能刷新。

程序如何在填充listview的情况下在2秒内加载,但是再次运行该方法需要更长的时间。

请参阅我附带的代码并提出任何更改建议,我为篇幅道歉。谢谢。

public void AllHomeworkers()
{
    //This updates the homeworkers listview to contain all the records from the homeworkers table.

    listHomeworkersAll.BeginUpdate(); //This uses the begin update process on the listview, this is used to stop flickering
    listHomeworkersAll.Items.Clear(); //Clears all the items from the listview

    // this takes the datatable returned from AllHomeworkers stored procedure.
    // It then loops through the datatable adding every row to the list view.

    DataTable dtHomeworkers = _businessLayer.AllHomeworkers();
    for (int i = 0; i < dtHomeworkers.Rows.Count; i++)
    {
        DataRow drowHomeworkers = dtHomeworkers.Rows[i];

        if (drowHomeworkers.RowState != DataRowState.Deleted)
        {
            ListViewItem lvi = new ListViewItem(drowHomeworkers["StaffID"].ToString());
            lvi.SubItems.Add(drowHomeworkers["Title"].ToString());
            lvi.SubItems.Add(drowHomeworkers["Initials"].ToString());
            lvi.SubItems.Add(drowHomeworkers["Forename"].ToString());
            lvi.SubItems.Add(drowHomeworkers["Surname"].ToString());
            lvi.SubItems.Add(drowHomeworkers["Address"].ToString());
            lvi.SubItems.Add(drowHomeworkers["Address2"].ToString());
            lvi.SubItems.Add(drowHomeworkers["City"].ToString());
            lvi.SubItems.Add(drowHomeworkers["Postcode"].ToString());
            lvi.SubItems.Add(drowHomeworkers["CostCentre"].ToString());
            lvi.SubItems.Add(drowHomeworkers["Email"].ToString());
            lvi.SubItems.Add(drowHomeworkers["Telephone"].ToString());
            lvi.SubItems.Add(drowHomeworkers["Mobile"].ToString());
            lvi.SubItems.Add(drowHomeworkers["FMID"].ToString());
            lvi.SubItems.Add(drowHomeworkers["Comments"].ToString());
            lvi.SubItems.Add(drowHomeworkers["Leaver"].ToString());
            lvi.SubItems.Add(drowHomeworkers["LeavingDate"].ToString());
            lvi.SubItems.Add(drowHomeworkers["Base"].ToString());
            lvi.SubItems.Add(drowHomeworkers["NextVisit"].ToString());
            var deleted = drowHomeworkers["Deleted"].ToString();
            if (deleted != "")
            {
                lvi.ForeColor = Color.Red;
            }
            listHomeworkersAll.Items.Add(lvi);
        }
    }
    listHomeworkersAll.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);  
    // this sorts the columns to the size of the largest object stored inside them.

    listHomeworkersAll.EndUpdate();
    // this ends the listview update process.
}

2 个答案:

答案 0 :(得分:0)

刷新前可能需要SuspendLayout(),填写完所有数据后需要ResumeLayout()

  

向父控件添加多个控件时,建议您在初始化要添加的控件之前调用SuspendLayout方法。将控件添加到父控件后,调用ResumeLayout方法。这将提高具有许多控件的应用程序的性能。

答案 1 :(得分:0)

我认为这可能会跑得更快......

    public void AllHomeworkers()
    {

        //This updates the homeworkers listview to contain all the records from the homeworkers table.

        listHomeworkersAll.BeginUpdate(); //This uses the begin update process on the listview, this is used to stop flickering
        listHomeworkersAll.Items.Clear(); //Clears all the items from the listview

        // this takes the datatable returned from AllHomeworkers stored procedure.
        // It then loops through the datatable adding every row to the list view.

        DataTable dtHomeworkers = _businessLayer.AllHomeworkers();

        List<ListViewItem> listHomeworkers = new List<ListViewItem>();

        for (int i = 0; i < dtHomeworkers.Rows.Count; i++)
        {
            DataRow drowHomeworkers = dtHomeworkers.Rows[i];

            if (drowHomeworkers.RowState != DataRowState.Deleted)
            {
                ListViewItem lvi = new ListViewItem(drowHomeworkers["StaffID"].ToString());
                lvi.SubItems.Add(drowHomeworkers["Title"].ToString());
                lvi.SubItems.Add(drowHomeworkers["Initials"].ToString());
                lvi.SubItems.Add(drowHomeworkers["Forename"].ToString());
                lvi.SubItems.Add(drowHomeworkers["Surname"].ToString());
                lvi.SubItems.Add(drowHomeworkers["Address"].ToString());
                lvi.SubItems.Add(drowHomeworkers["Address2"].ToString());
                lvi.SubItems.Add(drowHomeworkers["City"].ToString());
                lvi.SubItems.Add(drowHomeworkers["Postcode"].ToString());
                lvi.SubItems.Add(drowHomeworkers["CostCentre"].ToString());
                lvi.SubItems.Add(drowHomeworkers["Email"].ToString());
                lvi.SubItems.Add(drowHomeworkers["Telephone"].ToString());
                lvi.SubItems.Add(drowHomeworkers["Mobile"].ToString());
                lvi.SubItems.Add(drowHomeworkers["FMID"].ToString());
                lvi.SubItems.Add(drowHomeworkers["Comments"].ToString());
                lvi.SubItems.Add(drowHomeworkers["Leaver"].ToString());
                lvi.SubItems.Add(drowHomeworkers["LeavingDate"].ToString());
                lvi.SubItems.Add(drowHomeworkers["Base"].ToString());
                lvi.SubItems.Add(drowHomeworkers["NextVisit"].ToString());
                var deleted = drowHomeworkers["Deleted"].ToString();
                if (deleted != "")
                {
                    lvi.ForeColor = Color.Red;
                }
                listHomeworkers.Add((ListViewItem)lvi);
            }
        }

        ListViewItem[] arr = listHomeworkers.ToArray();

        listHomeworkersAll.Items.AddRange(arr);

        listHomeworkersAll.EndUpdate();
        // this ends the listview update process.
    }