2010-08-06 43 views
0

我有一个dataTable绑定到DatagrindView并且想要更新选定的列。我DataTable(displayDT)和我datagridview1具有相同的布局:更新绑定到Datagridview的DataTable时的性能问题

DateTime     item1   item2   item3 
1/1/2010     100    100    100 
1/1/2010 1:00    120    110    90 

我有1440条记录。我允许用户按选定的列更新数据......这样他们可以选择item1并为每条记录的值添加一个金额。这里是我的代码,我用它来运行更新:

在我的屏幕的负荷:

dataGridView1.DataSource = displayDT; 
bindingSourceDG.DataSource = displayDT; 

这里是我的代码,将更新inputed一个价值选择的列。

private void btnUpdateColumns_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     //user will key in a value that they would like 
     //the column to be increased/decreased by 
     double iValue = double.Parse(txtValue.Text); 

     this.Cursor = Cursors.WaitCursor; 

     //here I am trying to stop the datagridview 
     //from syncing up with the datatable after each record changes.  
     bindingSourceDG.RaiseListChangedEvents = false; 

     bindingSourceDG.SuspendBinding(); 

     //I created a list of columns the user would like 
     //to update by the amount they typed in. 
     List<int> colIndexList = new List<int>(dataGridView1.Columns.Count); 

     foreach (DataGridViewColumn col in dataGridView1.SelectedColumns) 
     { 
      colIndexList.Add(col.Index); 
     } 

     // here is my work around to get this process to work faster.... 
     //with this line of code(the filter) the DataTable will 
     //update in a second... 
     //without it takes much longer and this is what I 
     //do not understand...???? 
     //Why does it update so fast with the filter. 
     //With the filter applied the 1440 record go down 
     //to 24 records. So try a test with and without this line of code. 

     displayDT.DefaultView.RowFilter = 
      "1 = 1 AND DateTime = '" + iModelStartDate + "'"; 

     //I loop through all the rows in the displayDT and 
     //for each column selected I add the value keyed in by the user. 

     foreach (DataRow dr in displayDT.AsEnumerable()) 
     { 
      for (int counter = 0; counter < colIndexList.Count; counter++) 
      { 
       dr[colIndexList[counter]] = 
        (double)dr[colIndexList[counter]] + iValue; 
      } 
     } 

     // I clear the filter. 
     //But you would not need this if running without the "work around" 
     displayDT.DefaultView.RowFilter = ""; 

     dataGridView1.CurrentCell = null; 

     bindingSourceDG.RaiseListChangedEvents = true; 
     bindingSourceDG.ResetBindings(false); 

     dataGridView1.Refresh(); 
    } 
    catch 
    { 
     MessageBox.Show("Please enter numeric value.", "Enter Numeric"); 
    } 

    this.Cursor = Cursors.Default; 

} 

回答

0

好来回答这个问题(我想......它有点难以遵循),滤波器使事情更快的原因是因为它减少的处理的记录数。正如你说:

与过滤器应用的1440记录下井24条记录

因此,与那就只超过24条记录,而不是整个1440

编辑的过滤器

我怀疑它与你使用AsEnumerable()有关。通常它与LINQ类型过滤一起使用。我会冒险猜测,在手边设置过滤器会加速交互。我会测试这个假设,但直到今天晚些时候我才会得到时间。

+0

它更新所有1440条记录.... – Lenny 2010-08-06 18:22:59

+0

这是一个问题给你。是否有任何特殊的原因,你正在做displayDT.AsEnumerable()而不是displayDT.Rows? – 2010-08-06 18:42:02

+0

除此之外没有任何理由是我看到它完成的方式。今年我刚刚接触C#,所以仍然在学习。所以基于这个问题,我认为displayDT.Rows是一个更好的方法。 – Lenny 2010-08-06 18:49:49