DataGridView更新/刷新问题
本文关键字:新问题 刷新 更新 DataGridView | 更新日期: 2023-09-27 18:08:00
我有一个windows窗体,它有以下代码
BindingList<TicketResult> tickResults = new BindingList<TicketResult>();
BindingSource bindingSource1 = new BindingSource();
Action<String> call;
private void method(String x)
{
if (this.dataGridView1.InvokeRequired)
{
lock (this)
{
dataGridView1.Invoke(
new MethodInvoker(() =>
{
Debug.WriteLine(x);
tickResults[int.Parse(x)].Row = "first page";
dataGridView1.Refresh();
}));
}
}
}
public Form1()
{
call = method;
ServicePointManager.DefaultConnectionLimit = 48;
InitializeComponent();
tickResults.ListChanged += tickResults_ListChanged;
for (int i = 0; i < 10; i++)
{
TicketResult result = new TicketResult();
tickResults.Add(result);
}
bindingSource1.DataSource = tickResults;
dataGridView1.DataSource = bindingSource1;
for (int i = 0; i < 10; i++)
{
Search s = new Search();
int x = i;
Task.Run(() => s.start(x, this.call));
}
}
我不明白为什么不调用dataGridView1's
Refresh()
方法而不反映tickResults
的变化。
class Search : ISearch
{
public async Task<bool> start(int i, Action<String> x)
{
bool result = false;
TicketLogic tixLogic = new TicketLogic();
try
{
await Task.Run(() => tixLogic.processFirstPage(i, x))
.ContinueWith((t) => tixLogic.processSecondPage(i, x))
.ContinueWith((t) => tixLogic.processThirdPage(i, x));
result = true;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
result = false;
}
return result;
}
public async Task<bool> stop()
{
return false;
}
public async Task<bool> restart()
{
return false;
}
}
class TicketLogic
{
public async Task<bool> processFirstPage(int i, Action<String> x)
{
bool result = false;
try
{
HttpWebRequest request = WebRequest.CreateHttp("http://www.google.com");
WebResponse response = await request.GetResponseAsync();
StreamReader reader = new StreamReader(response.GetResponseStream());
String textResponse = await reader.ReadToEndAsync();
reader.Close();
response.Close();
result = true;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
result = false;
}
return result;
}
public async Task<bool> processSecondPage(int i, Action<String> x)
{
bool result = false;
try
{
HttpWebRequest request = WebRequest.CreateHttp("http://www.example.com");
WebResponse response = await request.GetResponseAsync();
StreamReader reader = new StreamReader(response.GetResponseStream());
String textResponse = await reader.ReadToEndAsync();
//tixResult.Information = "Second Page";
reader.Close();
response.Close();
x(i.ToString());
result = true;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
result = false;
}
return result;
}
public async Task<bool> processThirdPage(int i, Action<String> x)
{
bool result = false;
try
{
HttpWebRequest request = WebRequest.CreateHttp("http://www.hotmail.com");
WebResponse response = await request.GetResponseAsync();
StreamReader reader = new StreamReader(response.GetResponseStream());
String textResponse = await reader.ReadToEndAsync();
//tixResult.Information = "Third Page";
reader.Close();
response.Close();
x(i.ToString());
result = true;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
result = false;
}
return result;
}
}
在此之前,我尝试了另一种方法,其中我将数据绑定对象传递给计算任务,在那里数据绑定对象被操纵,但即使有结果是相同的,即在对象的变化没有反映在网格上,直到我点击网格中的一些单元格或最小化/最大化的形式。
我的问题是,为什么没有调用datagrid refresh()的变化没有反映在网格中?
尝试使用ObservableCollection代替BindingList这个Observable实现了INotifyPropertyChange,它会在某些东西发生变化时通知DataGridView
通常很难回答这样的问题,因为我们通常无法访问.NET
设计者的推理。
所以我试着通过猜测来理解它;我希望这不仅能帮助你理解,也能帮助你接受并充分利用它。
可能是他认为恒定的自动刷新对例如性能甚至用户体验不好。所以他们让你决定什么时候完成所有的更新来触发Refresh
..
Click
和代码调用之间有很大的区别,更不用说从其他任务调用了。一个Click
发生在UI上,所以UI应该是当前的。代码对数据源的更改可以连续发生多次,在任何频率下,闪烁的 UI将不太好。
让我们尝试改变视角:与其将手头的问题视为乏味的额外任务,不如将其视为控制刷新时间的机会。
或者进一步改变视角:当用户可能真的更喜欢刷新按钮和可能不引人注目的未完成更改或新记录计数时,您可以阻止用户被淹没。