如何迭代第1列上的单元格并更新第2列?

时间:2015-12-16 20:25:24

标签: c# datagridview

foreach(DataGridViewRow row in yourDataGridView.Rows)
{
    foreach(DataGridViewCell cell in row.Cells)
    {
        //do operations with cell
    }
}

我在第一个单元格中有一个主机名,在第二个单元格中有一个图像。

我想ping第一个单元格,如果我得到回复,那么我想用这两个图像中的一个更新第二列

Properties.Resources.online

Properties.Resources.offline

更新

第1列包含服务器名称 第2列是来自Resources的图像(显示绿色或红色)。这表示计算机是在线还是离线

第3列包含我们感兴趣的进程名称(如explorer.exe),以确保在线

第4列与#2类似,不过它的目的是告诉我们进程是否正在运行

我有代码来检查第3列中的流程

我也做了检查服务器是否在线的部分

我已经使用从按钮启动的Console.WriteLine进行了测试

2 个答案:

答案 0 :(得分:1)

如果我理解正确的话,我认为这样的事情应该有用。其中一些是伪代码,但我认为它应该是有道理的。

foreach (DataGridViewRow row in yourDataGridView.Rows)
{
    if (ping(row.Cells[0]))
    {
        row.Cells[1].Value = Properties.Resources.online;
    }
    else
    {
        row.Cells[1].Value = Properties.Resources.offline;
    }

    if (checkProcess(row.Cells[2]))
    {
        row.Cells[3].Value = Properties.Resources.online;
    }
    else
    {
        row.Cells[3].Value = Properties.Resources.offline;
    }
}

答案 1 :(得分:0)

foreach (DataGridViewRow row in yourDataGridView.Rows)
{
    if(ping(row.Cells[1]))
    {
        row.Cells[2].Value = Properties.Resources.online;
    }
    else
    {
        row.Cells[2].Value = Properties.Resources.offline;
    }
}
相关问题