5

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.

0

2 Answers 2

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

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

Sign up to request clarification or add additional context in comments.

6 Comments

Can you also preset column widths and stuff like that with attributes?
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.
(Sorry last question) Would I be able to bind to columns I have already set up using the designer?
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).
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.
|
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.