views:

1102

answers:

2

Hi,

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.

+4  A: 
 [DisplayName("Pages to print")]
 public long PagesToPrint {...}

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

Marc Gravell
Thanks for a fast answer!
Vidar
Can you also preset column widths and stuff like that with attributes?
Vidar
No, you can't. You can control some things like the type-converter (used to render and parse the value), but that is about it.
Marc Gravell
(Sorry last question) Would I be able to bind to columns I have already set up using the designer?
Vidar
I don't fully understand the question; but if the *designer* has already added the columns, no the [DisplayName] won't be used. If you use AutoGenerateColumns they will be (and I believe *new* designer uses will pick up the [DisplayName]). But it isn't hard to automate if you want a method you can run against a DataGridView to fix the column headers (let me know if you need this - I'd guess 10 lines).
Marc Gravell
Hi - sorry just come back from hols in Italy. What I meant - is I have set up columns in an order and format I like using the VS Forms Designer. I just wondered if you could bind to these manually created columns - I'm guessing you would have to make use of the ColumnAdded event for the dataGridView and do some jiggery pokery - not sure whether all this negates the benefits of data binding in the first place ??? Cheers.
Vidar
ahh, sorted it now by using the DataPropertyName property on each of the columns that binds it to your custom object.
Vidar
+1  A: 

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.

woany