Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# binding datagridviewcomboboxcolumn to list display, formatting, preferred Size error

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?

like image 206
Hilal Al-Rajhi Avatar asked Apr 21 '17 13:04

Hilal Al-Rajhi


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

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

like image 165
Fabio Avatar answered Sep 22 '22 04:09

Fabio