Customise DB Grid with web service data

Asked

Viewed 224 times

0

I am creating a simple system to test the operation of Delphi with Webservice REST, the first test is a database search that in this case is working all right, only that when the data is loaded in Dbgrid the view looks horrible, I wonder if you have how to customize the display of this information how to change the name and size of the columns.

Dbgrid:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

As you can see, it gets really bad with these giant fields for little information and these column titles get the name that comes from WS.

I used the REST Debugger tool to do this WS communication with Grid.

inserir a descrição da imagem aqui

  • 2

    At any event after the data is loaded (maybe at the end of the Search button event) do a for of the grid columns and check the size of the field by the dataset (e.g. string(200)), then calculate the column width, make for example 200 * 3 and you will have an approximate size appropriate to the field size.

  • 1

    Usually when the DBGrid is completed at runtime, itself defines the property Widthof each column. Given this, some get very large even size, in some cases it is even unnecessary. My suggestion: Leave fields in DBGrid configured so that when receiving the return from REST, just fill and do not configure.

  • @Lucasdesouzacruz my problem is just trying to leave already configured, since I can not add Fields in Tfdmemtable to be able to configure Fields on the grid, there is no other way to configure it, because I only know using a Query and a Datasource (Firedac).

  • 1

    Well, if you double-click on the Grid you can add the fields you need to display. Setting it up is easy, just inform the Title and the FieldName and ready.

  • @Lucasdesouzacruz then, the problem is that when I click on the Fieldname error: Cannot open dataset, I probably have to add a new component or configure something in Tfdmemtable, but anyway I was able to set up right the way I wanted using Andrey’s tip.

1 answer

2


At any event after the data is loaded (maybe at the end of the Search button event) do a for (repeat loop) of grid columns and check by dataset field size (e.g. string(200)), then calculate the width column, make for example 200 * 3 and vc will have an approximate size appropriate to the field size.

for i:=0 to dbgrid.columns.count -1 do
begin
  dbgrid.columns[i].width := dbgrid.datasource.dataset.fieldbyname(dbgrid.columns[i].fieldname).size * 3
end;

Of course the above code can be improved, it is just an idea of how to do. You can also see the field type (integer, varchar, etc) and change the multiplication value.

  • Complement to the Answer: Ideal to apply this logic in onDrawCell which is an automatic Grid loop.

  • @I don’t see the need to put onDrawCell, because the columns will not vary their size according to the content of the field, but rather according to the size in the database, so run only once right after the opening of the dataset is already enough. I understand that your suggestion is valid if the width of the column is conditional on the content of the field. Agree?

  • I agree, the suggestion was in relation to Em algum evento após os dados serem carregados. In the onDrawCell is elegant by being automatic and ALWAYS run on charging/recharging, no extra repeating structure is required for (this is my logical thinking for code decrease ;D). Anyway my +1 already given ;D

  • Oh cool, I understood your goal to put in the onDrawCell, really decreases the code, but in my view consumes system resources without need as it will run every time the grid is redesigned.. but it’s worth it. Thanks for the +1 and the suggestion. ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.