将GridControl绑定到SortedList

本文关键字:SortedList 绑定 GridControl | 更新日期: 2023-09-27 18:20:00

我正在尝试将一个表(特别是DevExpress GridControl)绑定到SortedList。我希望表的第一列绑定到SortedList的Key,第二列绑定到SortedList的键中对象的字段,例如

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        InitializeTable();
    }
    public void InitializeTable()
    {
        SortedList<DateTime, Dividend> EquityDividends = new SortedList<DateTime, Dividend>();
        EquityDividends.Add(new DateTime(2011, 1, 12, 16, 30, 00, DateTimeKind.Local), new Dividend(new DateTime(2011, 1, 12, 16, 30, 00, DateTimeKind.Local), "120", 12, new TimeSpan(4, 0, 0)));
        EquityDividends.Add(new DateTime(2011, 1, 13, 16, 30, 00, DateTimeKind.Local), new Dividend(new DateTime(2011, 1, 12, 16, 30, 00, DateTimeKind.Local), "125", 12, new TimeSpan(4, 0, 0)));
        gridControl1.DataSource = new BindingSource() { DataSource = EquityDividends };
        bandedGridView1.Columns[1].FieldName = "ExpectedDividend";
    }
}
public class Dividend
{
    public DateTime InDividendDate;
    public string ExpectedDividend;
    public double Adjustment;
    public TimeSpan TimeRemaining;
    public Dividend(
        DateTime InDividendDate,
        string ExpectedDividend,
        double Adjustment,
        TimeSpan TimeRemaining)
    {
        this.InDividendDate = InDividendDate;
        this.ExpectedDividend = ExpectedDividend;
        this.Adjustment = Adjustment;
        this.TimeRemaining = TimeRemaining;
    }
}

这不太管用(键出现在第0列,字符串"WindowsFormsApplication10.股息"出现在第1列)。有人有什么建议吗?

将GridControl绑定到SortedList

只需覆盖类Dividend的方法ToString()并返回您想要的值,如下所示:

public override string ToString()
{
    return "MyValue";
}

您将得到一列带有键,一列带有从ToString()返回的值。