DB grid not displaying column width correctly

159 Views Asked by At

I use DB Grids to display data, the problem is that the size of the columns are super inconsistent, and are in some cases way to big. Here I have a Db grid, width 450 displaying a field Player Name that in my access document is set to field size 20.

2 Db grids displaying the same field, but different sizes.

My database

As you can see the same field is shown, but displayed differently in the dbGrids. I have tried setting the size of the dbgrids columns manually by running this code:

DbGrid1.columns[0].width := 80;

But when I run this nothing happens, nothing is adjusted. The DbGrids are connected to datasources that are connected to ADO queries running SELECT statements with SQL. SQL code for left DB Grid:

 sSQL := 'SELECT PlayerName , PlayerSurname, GamesPlayed, GoalsScored, ' +
      'ROUND(GoalsScored/GamesPlayed ,2) AS GoalsPerGame ' +
      'FROM tblPlayers , tblStatistics ' +
      'WHERE tblPlayers.PlayerID = tblStatistics.PlayerID ' +
      'AND GamesPlayed <> 0 ' + 'UNION  ' +
      'SELECT PlayerName, PlayerSurname, GamesPlayed, GoalsScored, 0  ' +
      'FROM tblPlayers , tblStatistics ' +
      'WHERE tblPlayers.PlayerID = tblStatistics.PlayerID ' +
      'AND GamesPlayed = 0 ' + 'ORDER BY GoalsScored DESC';

I run this SQL statement. SQL statement for right dbGrid:

qryStatistics.SQL.Add
      ('SELECT PlayerName, PlayerSurname, PlayerSchoolID, GamesPlayed, GoalsScored, PlayerPosition  FROM tblStatistics, tblPlayers WHERE tblStatistics.PlayerID = tblPlayers.PlayerID');

The dbGrids dfm file looks like this:

object dbgScheduleS: TDBGrid
          Left = 40
          Top = 208
          Width = 609
          Height = 417
          DataSource = DM_Sport.dscSchedule
          ReadOnly = True
          TabOrder = 0
          TitleFont.Charset = ANSI_CHARSET
          TitleFont.Color = clWindowText
          TitleFont.Height = -11
          TitleFont.Name = 'Segoe Print'
          TitleFont.Style = []

Do you know how to set the size of these columns?

1

There are 1 best solutions below

0
Odin Mostert On

The problem was with my SQL statement, it seems like the UNION is what messed it up. The correct SQL was:

sSQL := 'SELECT PlayerName , PlayerSurname, GamesPlayed, GoalsScored, IIF(GamesPlayed = 0, 0, ROUND(GoalsScored/GamesPlayed ,2)) AS GoalsPerGame FROM tblPlayers , tblStatistics WHERE tblPlayers.PlayerID = tblStatistics.PlayerID ORDER BY GoalsScored DESC';

I still don't know how to change the column sizes after creating columns with the TADOQry, connected to a datasource, displaying to a dbGrid.