【问题标题】:Storing DataGrid in List将 DataGrid 存储在列表中
【发布时间】: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


    【解决方案1】:

    知道了。 您应该同时创建要添加的对象的实例,否则您只使用一个实例,而该实例恰好在您的周期中获得不同的值。当然,它会以您插入的最后一个值结束。

    public void ReadDataGridView(int ProcessId)
    {
        ProgramToWatchDataGrid updateGrid = null;
    
        //read and store every row
        int i=0;
        while(i<=totalRowsInGrid)
        {
            //create a new instance
            updateGrid = new ProgramToWatchDataGrid();
            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++;
        }
    }
    

    【讨论】:

    • 输入是,例如对于邮件 row0 christoferlindstrm@yahoo.com、row1 eliseisaksson@yahoo.com、row peter@pan.com。所以输出首先显示这些,但不知何故在我插入后它又丢失了。
    • 刚刚对其进行了测试,并且可以正常工作。这似乎也是合乎逻辑的事情,非常感谢!祝你有美好的一天。
    猜你喜欢
    • 2016-12-07
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    相关资源
    最近更新 更多