C#数据网格视图

C#数据网格视图,c#,winforms,datagridview,C#,Winforms,Datagridview,我有下面的代码,我正在ping一个web地址指定的次数,每次都将ping时间添加到一个名为resultsList的数组中 然后,我想将resultsList设置为数据网格视图的数据源 正在使用ping值填充resultsList 然而,它只是用2填充了我的数据网格视图 有什么想法吗 using System; using System.Collections.Generic; using System.Net; using System.ComponentModel; using System.

我有下面的代码,我正在ping一个web地址指定的次数,每次都将ping时间添加到一个名为resultsList的数组中

然后,我想将resultsList设置为数据网格视图的数据源

正在使用ping值填充resultsList


然而,它只是用2填充了我的数据网格视图

有什么想法吗

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;
        }
    }
}
使用系统;
使用System.Collections.Generic;
Net系统;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Net.NetworkInformation;
名称空间Ping_应用程序
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效pingButton\u单击(对象发送者,事件参数e)
{
列表结果列表=新列表();

对于(int indexVariable=1;indexVariable,您将绑定到每个字符串的长度。 您可以使用数据表而不是列表:

DataTable resultsList = new DataTable();
resultsList.Columns.Add("Time", typeof(String));
...
resultsList.Rows.Add(stat);

还有其他方法,但我认为这是最简单的,您可以命名列,并在需要时添加其他内容。

您绑定到每个字符串的长度。 您可以使用数据表而不是列表:

DataTable resultsList = new DataTable();
resultsList.Columns.Add("Time", typeof(String));
...
resultsList.Rows.Add(stat);

还有其他方法,但我认为这是最简单的,您可以命名该列,并在需要时添加其他内容。

如果您使用的是.NET framework 3.5或4.0,则可以向system.Linq添加using子句,并执行以下操作:

resultsGrid.DataSource = resultsList.Select(x => new { Value = x }).ToList();

或者,您可以使用字符串包装类:

public class StringWrapper
{
    public StringWrapper(string s)
    {
        Value = s;
    }
    public string Value { get { return _value; } set { _value = value; } }
    string _value;
}
然后声明变量,如下所示:

List<StringWrapper> resultsList = new List<StringWrapper>();
resultsList.Add(new StringWrapper(stat));
然后就可以绑定数据了,接下来就是:

resultsGrid.DataSource = resultsList;

如果您使用的是.NET framework 3.5或4.0,则可以将using子句添加到system.Linq并执行以下操作:

resultsGrid.DataSource = resultsList.Select(x => new { Value = x }).ToList();

或者,您可以使用字符串包装类:

public class StringWrapper
{
    public StringWrapper(string s)
    {
        Value = s;
    }
    public string Value { get { return _value; } set { _value = value; } }
    string _value;
}
然后声明变量,如下所示:

List<StringWrapper> resultsList = new List<StringWrapper>();
resultsList.Add(new StringWrapper(stat));
然后就可以绑定数据了,接下来就是:

resultsGrid.DataSource = resultsList;

“然而,它只是用2填充我的数据网格视图。”你是说用2行?还是用数字2?只是用数字2-重复了多少次我的Ping。这与我将数据传递到数据网格视图的方式有关?首先,你需要一个resultsGrid.DataBind()使数据显示。如果没有,请将其放在.DataSource行之后。您可以发布datagrid标记吗?“但是它只是用2填充我的数据网格视图。”你是说用两行还是用数字2?只是用数字2-重复了多少次我的Ping。这与我将数据传递到数据网格视图的方式有关?首先,你需要一个resultsGrid.DataBind()使数据显示。如果没有,请将其放在.DataSource行之后。是否可以发布datagrid标记?