當欄位增加且資料量變多,在載入資料時,會延遲好幾秒才顯示,若捲動datagridview捲軸,會發生不順閃爍現象。

解決方法一:使用虛擬填充模式(無效)

using System.Reflection;
public partial class Form1 : Form
{   
     private Customer customerInEdit;
     private System.Collections.ArrayList customers =
                   new System.Collections.ArrayList();
     private Customer customerInEdit;
     private int rowInEdit = -1;
     private bool rowScopeCommit = true;

}

private void Form1_Load(object sender, EventArgs e)
{
     this.dataGridView1.VirtualMode = true;
     this.dataGridView1.CellValueNeeded += new
            DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);
}

private void dataGridView1_CellValuePushed(object sender,
            System.Windows.Forms.DataGridViewCellValueEventArgs e)
{
            Customer customerTmp = null;
            if (e.RowIndex < this.customers.Count-1)
            {
                // If the user is editing a new row, create a new Customer object.
                if (this.customerInEdit == null)
                {
                    this.customerInEdit = new Customer(
                        ((Customer)this.customers[e.RowIndex]).CompanyName,
                        ((Customer)this.customers[e.RowIndex]).ContactName);
                }
                customerTmp = this.customerInEdit;
                this.rowInEdit = e.RowIndex;
            }
            else
            {
                customerTmp = this.customerInEdit;
            }

            // Set the appropriate Customer property to the cell value entered.
            String newValue = e.Value as String;
            switch (this.dataGridView1.Columns[e.ColumnIndex].Name)
            {
                case "contactNameColumn0":
                    customerTmp.CompanyName = newValue;
                    break;

                case "contactNameColumn1":
                    customerTmp.ContactName = newValue;
                    break;
            }
}

解決方法二:使用雙緩衝(成功解決問題)

using System.Reflection;

public Form1()
        {
            InitializeComponent();
            dgvDataView.MakeDoubleBuffered(true);
        }


public static class ControlExtentions
 {
        public static void MakeDoubleBuffered(this Control control, bool setting)
        {
            Type controlType = control.GetType();
            PropertyInfo pi = controlType.GetProperty("DoubleBuffered",
            BindingFlags.Instance | BindingFlags.NonPublic);
            pi.SetValue(control, setting, null);
        }
 }

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 門外漢 的頭像
    門外漢

    門外漢的筆記

    門外漢 發表在 痞客邦 留言(1) 人氣()