将 GridControl 绑定到 SortedList
我试图将一个表(特别是 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 技术交流群。

只需重写类
Dividend
的方法ToString()
并返回您想要的值,如下所示:您将获得一列包含键和一列包含从 < 返回的值代码>ToString()。
Just override the method
ToString()
of classDividend
and return the value you want like that:You will get one column with the key and one column with the value returned from
ToString()
.