将GridControl绑定到SortedList

时间:2012-01-12 06:16:56

标签: c# devexpress sortedlist

我正在尝试将一个表(特别是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.Dividend”出现在第1列中)。有没有人有任何建议?

1 个答案:

答案 0 :(得分:1)

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

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

您将获得一个包含密钥的列和一个包含从ToString()返回的值的列。