8

I have a table payments that has a null-able integer column named payMonth. I have the following class and list:

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

The problem comes here. When data is displayed in the dgv, the DataGridViewComboBoxColumn cmbMonth shows the number values (1,2,3, ...) not the month name ('Jan','Feb','Mar', ...). And when i click the dgv it display the error: formatting, display and some times formatting, preferredSize. When I remove the DataPropertyName property, this error goes but data are not displayed. Though, the payMonth values in the table are only in the list range or is null.

This is payments table:

enter image description here

enter image description here

Whats wrong?

6
  • I see nothing suspicious in your code sample. Do you have any formatting eventhandlers for DataGridVIew?
    – Fabio
    May 6, 2017 at 5:12
  • No handler, but default cell style May 6, 2017 at 5:14
  • What is type of payMonth column in payment table? Is it Int32 as in months list?
    – Fabio
    May 6, 2017 at 5:15
  • Can you show full exception message and possible stack trace?
    – Fabio
    May 6, 2017 at 5:15
  • In ms access it is of type integer May 6, 2017 at 5:19

1 Answer 1

6
+50

After your update

Database type of payMonth column is Number: integer

MS Access type Number: integer represent 16-bit integer - Int16 type in .Net.
So, you should change payMonth property to be short(Int16)

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

List of the Most Common Data Type Mappings

1
  • I am sure that the column name in the database table is the same as DataPropertyName value. It is payMonth. May 6, 2017 at 5:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.