在列表中存储DataGrid

时间:2013-09-16 23:34:22

标签: c# list datagrid datagridview process

使用我的程序,我正在尝试自动化另一个可能有多个实例的程序。我已经编写了一些功能,可以查看进程列表并检测我想要自动化的程序的所有进程。

它会将关于找到的实例的一些基本信息存储到这个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),将我读入的所有数据写入其中,并将其放入我的Dictionary(我简化)。为此,我遍历DataGrid(第一个while循环),row for row并将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 [basedRow],因为我在那个地方插入了信息。

奇怪的是,这个输出会产生:

[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

所以在我插入之后,List包含正确的值。但它会永远是读取的最后一个值吗?为什么会发生这种情况?我该如何解决这个问题?

非常感谢一些帮助!

1 个答案:

答案 0 :(得分:0)

知道了。 您应该在while中创建要添加的对象的实例,否则您只使用一个恰好在您的循环中获得不同值的实例。当然,它最终将会插入您输入的最后一个值。

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++;
    }
}