Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Problem with a Datagrid on a Window Form.

Am I doing something stupid, or is the vb.net datagrid control misbehaving?

I've created a window form with a datagrid on (and all the necessary stuff to connect to an access DB). I have a table called "Job Advert" which I am trying to get data from.

I used the query builder on the data adapter to create my query, and included aliases. i.e. my sql is:

SELECT
JobAdvert_PostedDate AS [Posted Date],
JobAdvert_Title AS [Job Title],
JobAdvert_RefCode AS [Job Ref],
JobAdvert_EnteredDate AS [Entered Date],
JobAdvert_PK
FROM
JobAdvert
ORDER BY
JobAdvert_PostedDate DESC,
JobAdvert_Title

However, when I run the query to fill the grid, I get 9 columns returned - the five fields from the table, all filled with nulls, and the four columns with aliases, all filled with the correct values.

What am I doing wrong? If it is a problem with the datagrid, is there another control or a workaround I could use?

Mark
Friday, September 19, 2003

sounds like you might have the grid set to autogenerate columns?

steveL
Friday, September 19, 2003

I found that setting, but it only applies to web forms, not windows forms, unfortunately.

Mark
Friday, September 19, 2003

Try checking the column names.

The dataset fill method can add "unmatched" columns to the datatable.

Names are also case sensitive.

Jonathan Lyall
Tuesday, October 07, 2003

Just thought about this a bit more.
A better soultion than aliasing in the select statement would be to return the column names in raw form.

Create a typed dataset for your select.
Add the aliases to the heading property in the dataset.datatable.
Fill the dataset with your adapter.

Iterate over the columns in the datatable that your grid is bound to.  Creating a DataColumnMapping for each column and setting the heading from the property in the Grid during the form load.

Bind the dataset as normal.

Jonathan Lyall
Tuesday, October 07, 2003

Jonathon,

Thanks for the suggestions. They sound like they should work - if only I'd got them sooner....

In the end I went with using tablestyles on the data grid to hide columns, based on what I learnt here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/wnf_custdatagrid.asp

Mark
Wednesday, October 08, 2003

*  Recent Topics

*  Fog Creek Home