How to adjust the DBGrid column width?
1. Set your DBGrid and adjust the column width after displaying the data as follows:
1. Double-click DBGrid and an edit box is displayed: editing dbgrid1.colunms.
2. If it is blank, click the third icon: add all fields
3. Select the row to be set. In its attribute bar, set the windth value you want.
Ii. DBGrid automatically adjusts column width
Function dbgridrecordsize (mcolumn: tcolumn): Boolean;
{Whether the maximum display width of the returned record data mesh is successful}
Begin
Result: = false;
If not assigned (mcolumn. Field) Then exit;
Mcolumn. Field. Tag: = max (mcolumn. Field. Tag,
TDBGrid (mcolumn. Grid). Canvas. textwidth (mcolumn. Field. displaytext ));
Result: = true;
End; {dbgridrecordsize} function dbgridautosize (mdbgrid: TDBGrid; moffset: integer = 5): Boolean;
{Whether the auto-adaptive width of the returned data mesh is successful}
VaR
I: integer;
Begin
Result: = false;
If not assigned (mdbgrid) Then exit;
If not assigned (mdbgrid. datasource) Then exit;
If not assigned (mdbgrid. datasource. dataset) Then exit;
If not mdbgrid. datasource. dataset. Active then exit;
For I: = 0 to mdbgrid. Columns. Count-1 do
Begin
If not mdbgrid. Columns [I]. Visible then continue;
If assigned (mdbgrid. Columns [I]. Field) then
Mdbgrid. Columns [I]. Width: = max (mdbgrid. Columns [I]. Field. Tag,
Mdbgrid. Canvas. textwidth (mdbgrid. Columns [I]. Title. Caption) + moffset
Else
Mdbgrid. Columns [I]. Width: =
Mdbgrid. Canvas. textwidth (mdbgrid. Columns [I]. Title. Caption) + moffset;
Mdbgrid. Refresh;
End;
Result: = true;
End; {dbgridautosize}
////// Source code end ///// use the example to start procedure tform1.dbgrid1drawcolumncell (Sender: tobject; const rect: trect;
Datacol: integer; column: tcolumn; State: tgriddrawstate );
Begin
Dbgridrecordsize (column );
End; Procedure tform1.button1click (Sender: tobject );
Begin
Dbgridautosize (dbgrid1 );
End;
////// End with the example
Uses math;
3. Using the INI file in Delphi to save the table column width
When using Delphi for database system development, it is inevitable to output query results in many places. One of the most frequently used controls for displaying query results is TDBGrid. However, the default display column width of the TDBGrid control is related to the maximum width of the corresponding fields in the database. For example, if the database has a "contact address" Field of Type varchar (200), the query results are displayed in unfriendly ways! This column is very wide. Moreover, in most cases, the width of each column is not what the designer or user wants. This must be recognized by system developers. Once, my solution was to call a custom process after each query operation to set the column widths of each table. These widths are also in the design phase, design and debug. This is not only troublesome, but also requires a lot of repetitive work, which is very inconvenient. In addition, this is very rigid: The program fixes the width of each column in the table! Actually, it is fixed by the designer! If you can provide an initial default value and set it by the user, it is certainly better. Therefore, a more efficient solution is to use the INI file to store the width of each column in each table. However, to facilitate reading and storage, it is best to use a number for the section name in each segment. This can be achieved through a loop statement. The INI file is used because it is very convenient to read and write the INI file. The second is to specify a return value when the file fails to be read. You can also use stringlist To Save settings to other text files. Generally, you need to use more than three tables. It is best to save the column width settings of each table in the same INI file and use the segment name to differentiate the tables. The following describes my own implementation methods. Process Definition: create a new form and reference dbgrids in the interface. Define two processes, setcolumnwidth and savecolumnwidth, to read/Save the table column width from/to the INIFILE. The two processes both require three parameters, respectively accepting the incoming TDBGrid, the segment name in the INI file and the INI file name containing the path; the segment name is used to distinguish different tables. The two processes are as follows: export procedure tgridform. setcolumnwidth (const grid: TDBGrid; Section, fname: string );
VaR
I: integer;
INIFILE: Tinifile; // call the file and specify the column width of dbgrid1 based on the content of the INI File
Begin
If fileexists (fname) Then // if the file exists
Begin
Try
INIFILE: = Tinifile. Create (fname );
For I: = 0 to grid. Columns. Count-1 do
Grid. Columns [I]. Width: = INIFILE. readinteger (section, inttostr (I), 150 );
Finally // If the read fails, 150 is returned as the default value. You can set the write according to the actual situation.
INIFILE. Free; // Translation
End;
End;
End; ----------------------------------------------------------------------------- procedure tgridform. savecolumnwidth (const grid: TDBGrid; Section, fname: string );
VaR
I: integer;
INIFILE: Tinifile; // The column width of dbgrid1 is recorded when the form is closed.
Begin
Try
INIFILE: = Tinifile. Create (fname );
For I: = 0 to grid. Columns. Count-1 do
INIFILE. writeinteger (section, inttostr (I), grid. Columns [I]. width );
// Write the width of each column, with the table name as the segment name and number as the segment name, starting from 0
// Write the current column width
Finally
INIFILE. Free;
End;
End; ----------------------------------------------------------------------------- for universality, this unit usually only writes some processes or functions that pass or are only used for internal calls for external calls. For example, if the form name is set to gridform and the unit file is saved as gridunit, you only need to add gridunit to its reference list when you need to call the form that passes the preceding procedure, you can just input the corresponding parameters when referencing them. Call: Call the process of setting the column width after querying the database. No matter whether the number of rows returned by the query is 0 or not, because when 0 rows are returned (no results are returned, the table title of DBGrid contains the returned title of the query: gridform. setcolumnwidth (dbgrid1, 'stock', extractfilepath (application. exename) + 'gridwidth. INI '); In the closing event of the main call form, save the current column width gridform. savecolumnwidth (dbgrid1, 'stock', extractfilepath (application. exename) + 'gridwidth. INI '). The second parameter specifies that the width of the read/save column is "inventory", and the third parameter specifies that the INI file is grdiwidth in the directory where the application is located. the INI file is searched for or stored in the application directory, but it is best not to use an absolute path. In this way, when a table displays data for the first time, it is still the default situation because the column width settings of the table are not stored in the INI file. Manual adjustment: After the form is closed, the next time you open the form, the column width will be set to the format of the previous manual adjustment, because the last adjustment result has been saved to the INI file. Next let's take a look at the content in this INI file: ------------------------------------------------------------------------- [employee]
0 = 96
1 = 184
2 = 65
3 = 112
4 = 124
5 = 124
6 = 123
7 = 108
[Business list]
0 = 64
1 = 184
2 = 304
3 = 82
4 = 64
5 = 6004
[Department]
0 = 75
1 = 138
2 = 184
3 = 127
[User]
0 = 184
1 = 85
2 = 110
3 = 177
[Supplier]
0 = 106
1 = 82
2 = 82
3 = 42
4 = 123
5 = 123
6 = 130
7 = 122
8 = 141
...... At the end of the last statement, we also need to say that the file to be saved is usually stored in the current directory and a subdirectory is created for storage. However, when reading the file, we need to determine whether the subdirectory exists, if it does not exist, create it first, especially Save it. Otherwise, an error may occur.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service