我有下面的代码,我正在 ping 一个网址指定的次数,每次都将 ping 时间添加到一个名为 resultsList 的数组中。
然后我想将 resultsList 设置为我的数据 GridView 的数据源。
resultsList 正在填充 ping 值。
然而,它只是用 2 填满了我的数据 GridView 。
有什么想法吗?
using System;
using System.Collections.Generic;
using System.Net;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
namespace Ping_Application
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pingButton_Click(object sender, EventArgs e)
{
List<string> resultsList = new List<string>();
for (int indexVariable = 1; indexVariable <= timesToPing.Value; indexVariable++)
{
string stat = "";
Ping pinger = new Ping();
PingReply reply = pinger.Send(pingAddressTextBox.Text);
if (reply.Status.ToString() != "Success")
stat = "Failed";
else
stat = reply.RoundtripTime.ToString();
pinger.Dispose();
resultsList.Add(stat);
}
resultsGrid.DataSource = resultsList;
}
}
}
非常感谢, J
请您参考如下方法:
您绑定(bind)到每个字符串的长度。 您可以使用 DataTable 而不是列表:
DataTable resultsList = new DataTable();
resultsList.Columns.Add("Time", typeof(String));
...
resultsList.Rows.Add(stat);
还有其他方法,但我认为这是最简单的,您可以为列命名,并且可以在需要时添加其他内容。
