将 GridControl 绑定到 SortedList

发布于 12-26 05:23 字数 1556 浏览 5 评论 0原文

我试图将一个表(特别是 DevExpress GridControl)绑定到 SortedList。我希望表的第一列绑定到 SortedList 的键,第二列绑定到 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 列中)。有人有什么建议吗?

I am trying to have a table (a DevExpress GridControl in particular) be bound to a SortedList. I want the first column of the table to be bound to the Key of the SortedList and the second column to be bound to a field of the object in the key of the SortedList, for example,

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;
    }
}

This doesn't quite work (the key comes up in column 0 and a string "WindowsFormsApplication10.Dividend" comes up in column 1). Does anyone have any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枯寂2025-01-02 05:23:24

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

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

您将获得一列包含键和一列包含从 < 返回的值代码>ToString()。

Just override the method ToString() of class Dividend and return the value you want like that:

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

You will get one column with the key and one column with the value returned from ToString().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文