简体   繁体   English

在Delphi中使用FDMemtable时,如何正确证明DBGrid标题的合理性?

[英]How do you right justify DBGrid titles when using a FDMemtable in Delphi?

I can't seem to get the fixed row Titles in a DBGrid to align right justified when using a FDMemtable. 使用FDMemtable时,我似乎无法在DBGrid中获得固定的行标题以右对齐。 Whenever I set the Field alignment to taRightJustify it right justifies the data cells perfectly. 每当我将“字段对齐方式”设置为taRightJustify时,它就正确地完美对齐了数据单元。 However, the DBGrid titles are always left justified. 但是,DBGrid标题总是左对齐。

What's even more frustrating is I can set the corresponding DBGrid column title alignment to taRightJustify and it appears perfectly fine in the IDE. 更令人沮丧的是,我可以将相应的DBGrid列标题对齐方式设置为taRightJustify,并且在IDE中看起来非常好。 But when I run the program the column title shows as left justified. 但是当我运行程序时,列标题显示为左对齐。

Has anyone found a way to make DBGrid column titles stay right justified when using a FDMemtable? 有没有人找到一种使用FDMemtable使DBGrid列标题保持正确的方法?

BTW, this also happens with taCenter. 顺便说一句,taCenter也会发生这种情况。 The data cells align centered but the titles stay left justified. 数据单元居中对齐,但标题保持左对齐。

PEBKAC 佩巴卡

The issue was of my own making. 这个问题是我自己造成的。 I did not invoke the DBGrid Columns Editor and add all the fields. 我没有调用DBGrid列编辑器并添加所有字段。 Instead, I was using the "Structure" pane and getting to the DBGrid columns that way. 相反,我使用的是“结构”窗格,并以这种方式进入DBGrid列。 Although the Structure pane allowed me to modify the column titles this was only temporary and did not persist when the program was run. 尽管“结构”窗格允许我修改列标题,但这只是临时的,并且在程序运行时不会持久。

The code below works for me in Seattle. 下面的代码在西雅图对我有用。 I'm using a TClientDataSet rather than a TFDMemTable, but I can't see that that would make any difference. 我使用的是TClientDataSet而不是TFDMemTable,但是我看不到那会有所作为。

If you have persistent columns defined on your DBGrid, you can also set a column's title alignment via the Object Inspector - use it to select the column, then expand its Title node and you can set the title alignment there. 如果在DBGrid上定义了永久列,则还可以通过对象检查器设置列的标题对齐方式-使用它来选择列,然后展开其Title节点,然后可以在此处设置标题对齐方式。

procedure TForm1.CDS1AfterOpen(DataSet: TDataSet);
var
  i : Integer;
begin
  for i := 0 to DBGrid1.Columns.Count - 1 do
    DBGrid1.Columns[i].Title.Alignment := taRightJustify;
end;

Btw, if you think you're setting the alignment in the OI but it's getting ignored, see if you can find out why, as follows: 顺便说一句,如果您认为要在OI中设置对齐方式,但是该对齐方式已被忽略,请查看是否可以找到原因,如下所示:

  • Make sure your form is saved, then right-click on it and select View as text . 确保您的表单已保存,然后右键单击它并选择“ View as text Then, in the IDE editor window, you can see whether the Alignment property is saved as you've specified in the OI. 然后,在IDE编辑器窗口中,您可以查看Alignment属性是否按在OI中指定的方式保存。 Use the editor context menu to return to viewing the form as a form. 使用编辑器上下文菜单返回以表单形式查看表单。

  • Add an override of the form's Loaded method as I've described in a comment. 如我在注释中所述,添加对窗体的Loaded方法的覆盖。 With a breakpoint on the inherited in Loaded s body, you can inspect the Alignment value before and after inherited is called. 通过在Loaded主体中inherited的断点,可以在调用inherited前后检查Alignment值。

Why am I suggesting looking into Loaded ? 为什么我建议调查Loaded Well, it is called after a form is streamed in from the DFM and is the routine where the run-time system finishes setting up the form. 好吧,它是在从DFM流入表单后调用的,并且是运行时系统完成表单设置的例程。 Sometimes, admittedly very rarely, another component (usually a 3rd-party one) misbehaves and causes strange behaviour of the properties of other compononents. 有时,非常少见的是,另一个组件(通常是第3方组件)行为不当,并导致其他组件的属性发生奇怪的行为。

I can't reproduce the issue using a TFDMemTable either. 我也无法使用TFDMemTable重现该问题。

I dropped a TFDMemTable , TDataSource , and TDBGrid on a new VCL application's main form, connected them as usual (the grid's datasource set to DataSource1 , the datasource's DataSet set to FDMemTable1 ), and then added the below code to the form's OnCreate event: 我将TFDMemTableTDataSourceTDBGrid放在新的VCL应用程序的主窗体上,照常连接它们(网格的数据源设置为DataSource1 ,数据源的DataSet设置为FDMemTable1 ),然后将以下代码添加到窗体的OnCreate事件:

procedure TForm3.FormCreate(Sender: TObject);
begin
  FDMemTable1.FieldDefs.Add('ID', ftInteger, 0, True);
  FDMemTable1.FieldDefs.Add('LastName', ftString, 20);
  FDMemTable1.FieldDefs.Add('FirstName', ftString, 20);
  FDMemTable1.FieldDefs.Add('Salary', ftCurrency);
  FDMemTable1.CreateDataSet;
  FDMemTable1.Active := True;
  FDMemTable1.AppendRecord([1, 'Smith', 'John', 30000]);
  FDMemTable1.AppendRecord([2, 'Jones', 'Jane', 40000]);
  FDMemTable1.AppendRecord([3, 'Doe', 'David', 2500]);
  DBGrid1.Columns[3].Alignment := TAlignment.taRightJustify;
  DBGrid1.Columns[3].Title.Alignment := TAlignment.taRightJustify;
end;

It also works correctly if I set everything up at designtime. 如果我在设计时进行了所有设置,它也可以正常工作。 Repeat the same setup steps I used above, but instead of using the code, use the following steps: 重复我上面使用的相同设置步骤,但不要使用代码,而要使用以下步骤:

  1. Select FDMemTable1 in the Object Inspector. 在对象检查器中选择FDMemTable1。 At the bottom of the OI, click the LoadFromFile link, and navigate to the BDS Samples data folder (by default, in C:\\Users\\Public\\Public Documents\\Embarcadero\\Studio\\17.0\\Samples\\Data) and select animals.fds . 在OI的底部,单击LoadFromFile链接,然后导航到BDS Samples数据文件夹(默认情况下,在C:\\ Users \\ Public \\ Public Documents \\ Embarcadero \\ Studio \\ 17.0 \\ Samples \\ Data中)并选择animal.fds (No specific reason for choosing that one, except it has a numeric field we can use for testing.) (没有选择它的特定原因,除了它有一个可用于测试的数字字段。)

  2. Right-click on the DBGrid, and choose Columns Editor , or click the ellipsis button on the DBGrid.Columns property in the Object Inspector. 用鼠标右键单击DBGrid,然后选择列编辑器 ,或单击对象检查器中DBGrid.Columns属性上的省略号按钮。 Right-click in the Columns Editor and choose Add all fields . 右键单击“列编辑器”,然后选择“ 添加所有字段”

  3. Select either the Size or Weight column, expand it's Title property, and set Alignment to taRightJustify . 选择SizeWeight列,展开其Title属性,并将Alignment设置为taRightJustify

  4. Run the application. 运行应用程序。 The column you modified in step #3 above has a right-aligned title. 您在上面的步骤3中修改的列的标题右对齐。 (Here I used the Size column.) (这里使用了“ 大小”列。)

尺寸列标题右对齐的网格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM