【发布时间】:2013-09-16 23:34:22
【问题描述】:
我正在尝试使用我的程序自动化另一个可以有多个实例的程序。我已经编写了可以监视进程列表并检测我想要自动化的程序的所有进程的功能。
它将有关找到的实例的一些基本信息存储到此 ConcurrentDictionary 中,其 ProcessId 为键,类 ProgramToWatch 为值:
public static ConcurrentDictionary<int, ProgramToWatch> ProgramToWatchDictionary = new ConcurrentDictionary<int, ProgramToWatch>();
public class ProgramToWatch
{
public string ListItemName { get; set; }
public Process BEProcess { get; set; }
public string BEMainWindowTitle { get; set; }
public Application BEApplication { get; set; }
public Window BEWindow { get; set; }
public bool isLoggedIn { get; set; }
public List<ProgramToWatchDataGrid> BEDataGrid = new List<ProgramToWatchDataGrid>();
}
现在我遇到问题的部分。 我想观看的节目有一个 DataGridView,我想将它复制到我的字典中。为此,我有 BEDataGrid 列表。该列表使用此类作为其类型:
public class ProgramToWatchDataGrid
{
public int ListID { get; set; }
public string Name { get; set; }
public string Mail { get; set; }
}
现在为了存储它,我创建了 ProgramToWatchDataGrid 的另一个实例(称为 updateGrid),将我读入的所有数据写入其中,并将其放入我的字典中(我简化了)。为此,我遍历 DataGrid(第一个 while 循环),逐行遍历并将 updateGrid 复制到我的 Dictionary - 在第二个 while 循环中,我显示要验证的值:
public void ReadDataGridView(int ProcessId)
{
ProgramToWatchDataGrid updateGrid = new ProgramToWatchDataGrid();
//read and store every row
int i=0;
while(i<=totalRowsInGrid)
{
updateGrid.ListId = DataGrid.Rows[i].Cells[0].Value;
updateGrid.Name = DataGrid.Rows[i].Cells[1].Value;
updateGrid.Mail = DataGrid.Rows[i].Cells[2].Value;
ProgramToWatchDictionary[ProcessID].BEDataGrid.Insert(i, updateGrid);
Display(ProgramToWatchDictionary[ProcessID].BEDataGrid[i].Mail);
}
Display("Elements: " + ProgramToWatchDictionary[ProcessID].BeDataGrid.Count);
//display every rows mail
i=0;
while(i<=totalRowsInGrid)
{
Display("HI: " + ProgramToWatchDictionary[ProcessID].BEDataGrid[i].Mail);
i++;
}
}
按照我的理解,我读取的信息行现在应该位于 ProgramToWatchDictionary[ProcessID].BEDataGrid[accordingRow] 因为我在那个地方插入了信息。
奇怪的是,会产生这个输出:
[01:19] christoferlindstrm@yahoo.com
[01:19] eliseisaksson@yahoo.com
[01:19] peter@pan.com
[01:19] Elements: 3
[01:19] HI: peter@pan.com
[01:19] HI: peter@pan.com
[01:19] HI: peter@pan.com
所以在我插入它之后,列表包含正确的值。但是之后它总是会是最后一个被读取的值?为什么会发生这种情况,我该如何解决?
我们将不胜感激!
【问题讨论】:
标签: c# list datagrid datagridview process