1

I'm working on a user interface for a database. For reasons I am copying database tables into my own dataset, and from there I am querying across those tables to join them. This works (except for some type problems, but that isn't the focus here), but I want the column headers of my DataGridView to have up to two words per header (for a pretty display).

Here's the query :

Dim query = _
            From bidInfo In dataset.Tables("BidInfo").AsEnumerable() _
            Join projectInfo In dataset.Tables("ProjectInfo").AsEnumerable() _
            On projectInfo.Field(Of String)("Project") Equals bidInfo.Field(Of String)("Project") _
            Join contractorInfo In dataset.Tables("ContractorInfo").AsEnumerable() _
            On contractorInfo.Field(Of String)("Contractor") Equals bidInfo.Field(Of String)("Contractor") _
            Join contactInfo In dataset.Tables("ContactInfo").AsEnumerable() _
            On contactInfo.Field(Of String)("Contractor") Equals bidInfo.Field(Of String)("Contractor") And contactInfo.Field(Of String)("Contractor Contact") Equals bidInfo.Field(Of String)("Contractor Contact")
            Select New With { _
                .BidNumber = projectInfo.Field(Of String)("BidNumber"), _
                .Project = projectInfo.Field(Of String)("Project"), _
                .TimeDue = projectInfo.Field(Of String)("TimeDue"), _
                .Architect = projectInfo.Field(Of String)("Architect"), _
                .ArchitectPhone = projectInfo.Field(Of String)("ArchitectPhone")}

I then use

dataGridView1.DataSource = query.ToList

This all shows up nice except for the two-word column headings (BidNumber, TimeDue, ArchitectPhone). How do I get a space in them? I've tried having brackets on the member names and such, but it doesn't like that.

There must be a syntax I'm missing, but I haven't found it from my limited research (generally people say that spaces in field names are a bad idea, but this is for display).

As a second part, why does the DataGridView display nothing when I have a field(Of Double)?

Thank you in advance for any help.

EDIT: I've found a temporary solution in a question that was already asked (How to Set Custom DataGridView Headers (with spaces) using LINQ?) that it seems I have duplicated =/ The solution provided manually goes through the columns and re-sets the headers. This has the same outcome I want, but I would prefer to minimize the number of things I have to change if I add a column to the database. If anyone has a way to just start with the correct column headers, that would be great. If not, no worries, and thanks for taking the time to read all the text =)

Community
  • 1
  • 1
Hynch
  • 69
  • 1
  • 8
  • 1
    If you don't use an anonymous type, you can specify the `DisplayName` as in [this post](http://stackoverflow.com/a/6228673/1210053) – Nico Schertler Jun 04 '14 at 17:58
  • That's pretty neat, I didn't know about that. However, I am using an anonymous type. I'm going to use Mark's answer, it works well for me. Thank you for your help! – Hynch Jun 04 '14 at 18:18

1 Answers1

0

Nico's comment looks like the best way, but you could also use an ugly hack if you don't want to define a class. Since VB.NET property names can't have spaces, put an underscore where you want a space - e.g. .Bid_Number. Then (not tested, pseudo-code)...

dataGridView1.DataSource = query.ToList()
For Each col In dataGridView1.Columns
    If col.Name.Contains("_") Then
        col.HeaderText = col.Name.Replace("_", " ")
    End If
Next
Mark
  • 8,140
  • 1
  • 14
  • 29
  • Nico's does seem like the most professional way, but I'm going to go with yours =) Thank you for helping! – Hynch Jun 04 '14 at 18:19