C#绑定datagridviewcomboboxcolumn列出显示,格式化,首选大小错误

时间:2017-04-21 13:07:32

标签: c# list binding datacolumn

我有一个表付款,其中包含一个名为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值仅在列表范围内或为空。

这是付款表:

enter image description here

enter image description here

怎么了?

1 个答案:

答案 0 :(得分:6)

更新后

  

payMonth列的数据库类型是Number:integer

MS Access类型Number: integer表示.Net中的16位整数 - Int16类型 因此,您应该将payMonth属性更改为short(Int16)

public class months
{
    public short payMonth { get; set; }
    public string monthName { get; set; }
}

List of the Most Common Data Type Mappings