我有一个dataTable
与DatagrindView
绑定,并希望更新所选列。我的DataTable(displayDT)
和我的datagridview1
具有相同的布局:
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;
这是我的代码,它将使用输入的值更新所选列。
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 :(得分:0)
很好地回答这个问题(我认为......这有点难以理解),过滤器使事情变得更快的原因是因为它减少了处理的记录数量。如你所说:
应用过滤器后,1440记录下降到24条记录
因此,对于过滤器,它只会超过24条记录,而不是整个1440条。
修改强>
我怀疑它与您使用AsEnumerable()有关。通常,它与LINQ类型过滤一起使用。我冒昧地猜测,手动设置过滤器可以加快交互速度。我会测试这个hypothisis,但我不会得到时间直到今天晚些时候。