DataGridView仅编辑最后一列,以前的数据为空

时间:2017-03-10 10:27:56

标签: c# datagridview ping

我对DataGridView有疑问。我在DB表中保存了两个IP地址。我加载这些列并进行Async Ping调用。

我想在DataGridView中显示结果。但是当所有过程结束时,结果仅显示第二列。即使成功运行,以前的列也是空的。

我试图在很多方面做到这一点,但结果总是一样的。数据仅显示最后一个运行步骤,之前带有ping结果的列将被覆盖为空。 有人可以告诉我吗?

非常感谢,这是我的代码:

{
    InitializeComponent();
}

private void VPN_Load(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection("Data Source=CZPRGVMSLSQL01;Initial Catalog=TOOL;User ID=sa;Password=***");
    da = new SqlDataAdapter("select STORE,servername,NAS from stores where country=" + MyClass.MyClass1.ReturnCountry() + " order by store", conn);
    dt = new DataTable();

    da.Fill(dt);

    dataGridView1.DataSource=dt;

    foreach(DataGridViewRow row in this.dataGridView1.Rows)
    {

        if (row.IsNewRow)
        {
            continue;
        }
        count2 = 1;
        string data = row.Cells["servername"].Value.ToString();
        string datanas = row.Cells["NAS"].Value.ToString();

        // string data = (string)dataGridView1[3, Row].Value.ToString();
        Ping ping = new Ping();
        i++;
        ping.SendAsync(datanas, 2000, row);

        ping.PingCompleted += new PingCompletedEventHandler(ping_PingCompleted);

        System.Threading.Thread.Sleep(3);    
    }

    foreach (DataGridViewRow row in this.dataGridView1.Rows)
    {

        if (row.IsNewRow)
        {
            continue;
        }
        count2 = 2;
        string data = row.Cells["servername"].Value.ToString();
        string datanas = row.Cells["NAS"].Value.ToString();

        // string data = (string)dataGridView1[3, Row].Value.ToString();
        Ping ping = new Ping();
        i++;
        ping.SendAsync(data, 2000, row);
        ping.PingCompleted += new PingCompletedEventHandler(ping_PingCompleted);

        System.Threading.Thread.Sleep(3);
    }
}


void ping_PingCompleted(object sender, PingCompletedEventArgs e)
{

    var reply = e.Reply;

    DataGridViewRow row = e.UserState as DataGridViewRow;

    if (reply != null && row.Index > -1 )
    {

        if (reply.Status.ToString() == "Success")
        {
            dataGridView1[count2, row.Index].Style.BackColor = Color.Green;
            dataGridView1[count2, row.Index].Value = reply.Status;
        }
        else
            dataGridView1[count2, row.Index].Style.BackColor = Color.Red;

        dataGridView1[count2, row.Index].Value = reply.Status;
    }
    else if (row.Index>-1)
    {
        dataGridView1[count2, row.Index].Value = Color.Red;
        dataGridView1[count2, row.Index].Value = "Unexpected error";
    }
}

1 个答案:

答案 0 :(得分:0)

我看到的问题是您指望使用单个变量count2来确定要在PingCompleted处理程序中访问哪个列。此逻辑失败,因为当第一组Ping对象运行时(期望count2==1),您的第二个循环将已执行并设置count2=2

要修复,只需发送所需的单元格,而不是将整个row发送到ping.SendAsync

// count2 = 1;
// ...
ping.SendAsync(datanas, 2000, row.Cells["servername"]);

// count2 = 2;
// ...
ping.SendAsync(data, 2000, row.Cells["NAS"]);

然后像这样修改PingCompleted

void ping_PingCompleted(object sender, PingCompletedEventArgs e)//, int column)
{
    var reply = e.Reply;

    DataGridViewCell cell = e.UserState as DataGridViewCell;

    if (reply != null && cell.RowIndex > -1)
    {
        if (reply.Status.ToString() == "Success")
        {
            cell.Style.BackColor = Color.Green;
        }
        else
        {
            cell.Style.BackColor = Color.Red;
        }

        cell.Value = reply.Status;
    }
    else if (cell.RowIndex > -1)
    {
        cell.Style.BackColor = Color.Red;
        cell.Value = "Unexpected error";
    }
}

确保您发送的是正确的单元格和数据。根据我的判断,您将NAS单元格字符串值传递给Async方法,您希望将其与索引为1(不是NAS列)的列的单元格进行比较,反之亦然。我无法测试这个逻辑是正确还是反转,所以要密切关注它。