I have the below code, I am pinging a web address for a specified number of times, each time adding the ping time to an array called resultsList.
I then want to set resultsList as the data source for my Data Grid View.
The resultsList is being populated with ping values.
However it simply fills up my Data Grid View with 2’s.
Any ideas?
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;
}
}
}
Many Thanks,
J
You are binding to the length of each string.
You can use a DataTable instead of a list:
There are other ways, but I think this is simplest and you can name the column and you can add other stuff when you need to.