我正在ListView
使用来自 SQL 的两个数据集填充 a。这每 15 秒发生一次。如何保持滚动到位置并仍然刷新数据?我在这里看到了一些与相关问题有关的问题,但没有一个真正解决我的具体问题。我尝试了 TopMost 选项,但效果有限。
关于如何保持滚动到位置的任何建议?这是我用来填充数据的一些代码ListBox
。
private void PopulateData()
{
_agent.Stop();
listView1.Items.Clear();
listView1.Groups.Clear();
listView1.BeginUpdate();
string filter;
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
dt1.Columns.Add("Name", typeof(string));
dt1.Columns.Add("Status", typeof(string));
dt1.Columns.Add("Time", typeof(double));
dt1.Columns.Add("Calls", typeof(double));
dt1.Columns.Add("InProgress", typeof(double));
dt1.Columns.Add("Region", typeof(string));
dt2.Columns.Add("Name", typeof(string));
dt2.Columns.Add("CChats", typeof(double));
dt2.Columns.Add("AChats", typeof(double));
foreach (DataRow dr in _agentStates.Rows)
{
DataRow row = dt1.NewRow();
row["Name"] = dr[0].ToString();
row["Status"] = dr[1].ToString();
row["Time"] = Convert.ToDouble(dr[2].ToString());
row["Calls"] = Convert.ToDouble(dr[3].ToString());
row["InProgress"] = Convert.ToDouble(dr[4].ToString());
row["Region"] = dr[5].ToString();
dt1.Rows.Add(row);
}
foreach (DataRow dr in _chatCount.Rows)
{
DataRow row = dt2.NewRow();
row["Name"] = dr[0].ToString();
row["CChats"] = Convert.ToDouble(dr[1].ToString());
row["AChats"] = Convert.ToDouble(dr[2].ToString());
dt2.Rows.Add(row);
}
var result = from table1 in dt1.AsEnumerable()
join table2 in dt2.AsEnumerable()
on (string)table1["Name"] equals (string)table2["Name"]
into joinedDt
from table2 in joinedDt.DefaultIfEmpty()
select new
{
Name = (string)table1["Name"],
Status = (string)table1["Status"],
Time = (double)table1["Time"],
Calls = (double)table1["Calls"],
InProgress = (double)table1["InProgress"],
Region = (string)table1["Region"],
CChats = (table2 != null ? (double)table2["CChats"] : 0),
AChats = (table2 != null ? (double)table2["AChats"] : 0)
};
foreach (var item in result)
{
if (item.Status != "NLO" && item.Status !="Webchat Account")
{
var calls = item.Calls + item.CChats;
var lvi = new ListViewItem(item.Name);
lvi.SubItems.Add(item.Status);
lvi.SubItems.Add(Conv.Time(item.Time));
lvi.SubItems.Add(item.Calls.ToString());
lvi.SubItems.Add(item.CChats.ToString());
lvi.SubItems.Add((item.AChats + item.InProgress).ToString());
lvi.SubItems.Add(calls.ToString());
this.listView1.Items.Add(lvi);
}
}
listView1.EndUpdate();
_agent.Start();
}