将链接列表绑定到数据网格视图

本文关键字:数据网 网格 视图 数据 链接 列表 绑定 | 更新日期: 2023-09-27 18:14:26

我正在尝试将链接列表绑定到数据网格视图。下面的方法适用于类中除数组之外的属性。如果我将数组声明为一个新实例,那么linkedlist就会正确创建,但数组没有绑定到datagridview中。如果数组是作为属性创建的(我认为代码是正确的(,则在创建linkedlist时会导致An unhandled exception of type 'System.StackOverflowException' occurred

谢谢你的帮助。

    public class PayoffNode
                {
                    public int DealNo { get; set; }
                    public string Category { get; set; }
                    public string Strategy { get; set; }
                    public string GreekType { get; set; }
                    // declare array as instance or as a property?
                    //public double[] Data = new double[22];  
                    public double[] Data
                    {
                        get { return Data; }
                        set { Data = value; }
                    }
                }
    LinkedList<Globals.PayoffNode> PayLL = new LinkedList<Globals.PayoffNode>();
                Random Rnd = new Random();
                for (int K = 1; K <= 10; K++)
                {
                    var T = new Globals.PayoffNode();
                    T.Category = "Account==" + K;
                    T.GreekType = "Greek==" + K;
                    T.DealNo = K;
                    T.Strategy = "Strategy==" + K;
                    for (int DP = 1; DP <= 21; DP++)
                    {
                        T.Data[DP] = Rnd.Next(10, 99);
                    }
                    PayLL.AddLast(T);
                }
                List<Globals.PayoffNode> qP = (from P in PayLL
                                               where P.Category == "Account==4" && P.DealNo == 4 && P.GreekType == "Greek==4" && P.Strategy == "Strategy==4"
                                               select P).ToList();
     PayoffTable.DataSource = qP;

更新:谢谢你的评论,这似乎奏效了。

public class PayoffNode
            {
                public int DealNo { get; set; }
                public string Category { get; set; }
                public string Strategy { get; set; }
                public string GreekType { get; set; }
                public double Data1 { get; set; }
                public double Data2 { get; set; }
                public double Data3 { get; set; }
                public double[] Data = new double[22];
            }
LinkedList<Globals.PayoffNode> PayLL = new LinkedList<Globals.PayoffNode>();
            Random Rnd = new Random();
            for (int K = 1; K <= 10; K++)
            {
                var T = new Globals.PayoffNode();
                T.Category = "Account==" + K;
                T.GreekType = "Greek==" + K;
                T.DealNo = K;
                T.Strategy = "Strategy==" + K;
                for (int DP = 1; DP <= 21; DP++)
                {
                    T.Data[DP] = Rnd.Next(10, 99);
                }
                PayLL.AddLast(T);
            }
List<Globals.PayoffNode> qP = (from P in PayLL
                                           where P.Category == "Account==4" && P.DealNo == 4 && P.GreekType == "Greek==4" && P.Strategy == "Strategy==4"
                                           select new Globals.PayoffNode()
                                           {
                                               Category=P.Category,
                                               DealNo=P.DealNo,
                                               GreekType=P.GreekType,
                                               Strategy=P.Strategy,
                                               Data1=P.Data[1],
                                               Data2 = P.Data[2],
                                               Data3 = P.Data[3],
                                           }).ToList();
PayoffTable.DataSource = qP;

将链接列表绑定到数据网格视图

避免生成21Data属性的一种方法是将List转换为DataTable:

class PayoffNode
{
    public int DealNo;
    public string Category;
    public double[] Data; // = new double[21];
}

然后

Random Rnd = new Random();
List<PayoffNode> PayLL = Enumerable.Range(1, 10).Select(i => new PayoffNode {
    DealNo = i,
    Category = "Account==" + i,
    Data = Enumerable.Range(1, 21).Select(d => (double)Rnd.Next(10, 99)).ToArray()
}).ToList();
// List<PayoffNode> to DataTable
var dt = new DataTable();
dt.Columns.Add("DealNo", typeof(int));
dt.Columns.Add("Category"); // typeof(string) by default
for (int i = 1; i <= 21; i++)
    dt.Columns.Add("Data" + i, typeof(double));
foreach (var P in PayLL)
{
    var dr = dt.Rows.Add(P.DealNo, P.Category);
    for (int i = 0; i < 21; i++)
        dr[i+2] = P.Data[i]; // +2 is the number of fields before the Data fields
}
PayoffTable.DataSource = dt;
dt.DefaultView.RowFilter = " Category = 'Account==4' ";

优点是只设置一次DataSource,只需更改RowFilter即可对其进行过滤。此外,对DataGridView所做的任何更改都会更改DataTable,反之亦然。

请注意,C#和大多数其他语言中的数组从0开始,而不是从1开始(.Data[0]用于访问数组中的第一项(,因此在我的示例中,用于访问Data数组的for循环是从0到20。

public double[] Data
{
    get { return Data; }
    set { Data = value; }
}

这就是问题所在,set和get方法中的对象需要引用不同的对象,否则无论何时设置对象值或试图检索对象值,它都会无限地调用这些方法。应该是…

private double[] _data;
public double[] Data
{
    get { return _data; }
    set { _data = value; }
}

或者。。。

public double[] Data { get; set; }