我有一个名为payments的表,其中有一个可为空的整数列名为payMonth。我有以下类和列表:
public class months
{
public int payMonth { get; set; }
public string monthName { get; set; }
}
lstMonths = new List<months> {
,new months() { payMonth = 1, monthName = "jan" }
,new months() { payMonth = 2, monthName = "feb" }
,new months() { payMonth = 3, monthName = "mar" }
,new months() { payMonth = 4, monthName = "apr" }
,new months() { payMonth = 5, monthName = "may" }
,new months() { payMonth = 6, monthName = "jun" }
,new months() { payMonth = 7, monthName = "jul" }
,new months() { payMonth = 8, monthName = "aug" }
,new months() { payMonth = 9, monthName = "sep" }
,new months() { payMonth = 10, monthName = "oct" }
,new months() { payMonth = 11, monthName = "nov" }
,new months() { payMonth = 12, monthName = "dec" }};
cmbMonth = new DataGridViewComboBoxColumn();
cmbMonth.DataSource = lstMonths;
cmbMonth.ValueMember = "payMonth";
cmbMonth.DisplayMember = "monthName";
cmbMonth.DataPropertyName = "payMonth";
cmbMonth.Name = "cmbPayMonth";
cmbMonth.HeaderText = "Month";
cmbMonth.FlatStyle = FlatStyle.Flat;
cmbMonth.Width = 80;
dgvPayments.Columns.Insert(5, cmbMonth);
问题出在这里。当数据显示在dgv中时,DataGridViewComboBoxColumn cmbMonth 显示数字值(1、2、3、...),而不是月份名称('Jan'、'Feb'、'Mar',...)。当我点击dgv时,它显示错误:格式化、显示和有时格式化、preferredSize。当我删除 DataPropertyName 属性时,这个错误消失了,但数据没有显示出来。尽管表中的 payMonth 值仅在列表范围内或为空。
这是支付表:
有什么问题吗?
DataGridVIew
编写任何格式化事件处理程序吗? - FabiopayMonth
列是什么类型?它是否与月份列表中的Int32
类型相同? - Fabio