我从sql server中填充了datagridview。我也创建了一个使用双缓冲属性的类。
public static class ExtensionMethods
{
public static void DoubleBuffered(this DataGridView dgv, bool setting)
{
Type dgvType = dgv.GetType();
PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(dgv, setting, null);
}
}
加载事件中的内部表单
private void CustomerTrans_Load(object sender, EventArgs e)
{
CustomersGrid.DoubleBuffered(true);
try
{
using (SqlConnection connection = new SqlConnection(Connection.connectionString))
using (SqlDataAdapter sda = new SqlDataAdapter("select TrnDocumentID,CustomerID,CustomerName,DateTime,Datetime,TrnName,SumQuantity,SumTotal,SumDiscountTotal,TableName,UserName,UserID from CustomerTrans where DateTime >= '" + DateTime.Now.ToString("M/d/yyyy") + "'", connection))
{
DataTable dt = new DataTable();
sda.Fill(dt);
CustomersGrid.Rows.Clear();
foreach (DataRow item in dt.Rows)
{
int n = CustomersGrid.Rows.Add();
CustomersGrid.Rows[n].Cells[0].Value = item[0].ToString();
CustomersGrid.Rows[n].Cells[1].Value = item[1].ToString();
CustomersGrid.Rows[n].Cells[2].Value = item[2].ToString();
CustomersGrid.Rows[n].Cells[3].Value = Convert.ToDateTime(item[3]).ToString("dd/MM/yyyy");
CustomersGrid.Rows[n].Cells[4].Value = Convert.ToDateTime(item[4]).ToString("HH:mm");
CustomersGrid.Rows[n].Cells[5].Value = item[5].ToString();
CustomersGrid.Rows[n].Cells[6].Value = Convert.ToDecimal(item[6].ToString());
CustomersGrid.Rows[n].Cells[7].Value = Convert.ToDecimal(item[7].ToString());
CustomersGrid.Rows[n].Cells[8].Value = Convert.ToDecimal(item[8].ToString());
CustomersGrid.Rows[n].Cells[9].Value = item[9].ToString();
CustomersGrid.Rows[n].Cells[10].Value = item[10].ToString();
CustomersGrid.Rows[n].Cells[11].Value = item[11].ToString();
}
}
}
catch (Exception ex)
{
}
我不知道原因,但是在滚动datagridview时性能仍然很慢。还有什么我想念的吗?