Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Databinding with a custom object to datagridview - Column Header Text

I have an list of objects (PrintJob) that I bind to a DataGridView. Here is a cut down version of the PrintJob object (Don't want to bore you completely!!):

public class PrintJob 
{
        private long pagesToPrint;
        public long PagesToPrint
        {
            get { return pagesToPrint; }
        }

        private long recipientRef;
        public long RecipientRef
        {
            get { return recipientRef; }
            set { recipientRef = value; }
        }
}

and I make a list of these objects and bind to the dataGridView like so:

dataGridView1.DataSource = uiModel.GetPrintJobs();

all good so far?

Everything displays fine expcept the Column Headers - which show the exact same as the Propery name in my object i.e. "PagesToPrint" appears in column header, where ideally, I would want it to display "Pages To Print" in the header text.

How do I get the Column Header text to show something a bit more readable - I guess based on the property name.

Cheers.

like image 888
Vidar Avatar asked Jun 11 '09 09:06

Vidar


2 Answers

 [DisplayName("Pages to print")]
 public long PagesToPrint {...}

etc (with using System.ComponentModel; at the top of the code file)

like image 113
Marc Gravell Avatar answered Oct 31 '22 17:10

Marc Gravell


Yes you can use the designer with a bound data source. Just set the "DataPropertyName" property value of each column in the designer.

For example...for column 1 the "DataPropertyName" property value would be "PagesToPrint", whilst the "HeaderText" property would be the actual text ("Pages to Print"?) you want displayed as the column header.

like image 21
woany Avatar answered Oct 31 '22 18:10

woany