如何在DBGrid中显示与数据库中的值不同的值?

时间:2021-11-25 20:12:35

PROBLEM

如何在DBGrid中显示与数据库中的值不同的值?

I have DBGdrid and there are columns what show ID's from an MS Access database. How can I change them to real values like item name, client name, employee name?

我有DBGdrid,有些列显示来自MS Access数据库的ID。如何将它们更改为项目名称,客户名称,员工姓名等实际值?

I have code (test code, I just tried to get all items names from table and I could save them to array or variable and change with DBGrid values what show id's), but I don't know how to change DBGrid value fields.

我有代码(测试代码,我只是试图从表中获取所有项目名称,我可以将它们保存到数组或变量,并使用显示id的DBGrid值更改),但我不知道如何更改DBGrid值字段。

procedure TForm2.Button1Click(Sender: TObject);
var i,j:integer; mas:string;
begin
Button1.Enabled := false;
Button2.Enabled := true;
Button3.Enabled := true;

Form1.ADOQuery1.SQL.Text := 'SELECT * FROM items_specification';

Form1.ADOQuery1.Open;
j:= Form1.ADOQuery1.RecordCount;
Form1.ADOQuery1.Close;

i:=1;

repeat
Form1.ADOQuery1.SQL.Text := 'SELECT * FROM items_specification WHERE item_id = :ID';
Form1.ADOQuery1.Parameters.ParamByName('ID').Value := i;
Form1.ADOQuery1.Open;
mas:= Form1.ADOQuery1['item_name'];
Form1.ADOQuery1.Close;
inc(i);
ShowMessage(mas) ;
until (i = j+1);

Maybe you have any suggestions how to solve the problem, I will appreciate that.

也许您对如何解决问题有任何建议,我将不胜感激。

In MS Access I have made look up to show names there, mby there is some way to do that in DBGrid?

在MS Access中我已经查找了显示名称,mby有一些方法可以在DBGrid中执行此操作吗?

UPDATED

But code doesn't matter.. My big question is how to set item name, client name and employee name in DBGrid (in values not column title)?! In MS Access those fields where is id is number, so if I even edit DBGrid it don't allow me to change value to string.. Only way what I could imagine is to broke relationships in MS Access and change fields to ShortText, but I think is not the best way.

但代码并不重要..我的重要问题是如何在DBGrid中设置项目名称,客户名称和员工姓名(值不是列标题)?!在MS Access中那些id为number的字段,所以如果我甚至编辑DBGrid,它不允许我将值更改为字符串..只有我能想象的方法是破坏MS Access中的关系并将字段更改为ShortText,但是我认为这不是最好的方式。

1 个解决方案

#1


4  

To show values in a DBGrid cell that are not actually in your database, you can add a calculated field.

若要在DBGrid单元格中显示实际上不在数据库中的值,可以添加计算字段。

You double click on the source: ADOQuery1 and add all the fields you want to the available field list. Then you add a new field.
(I've left the other fields empty because I'm lazy, but you should make sure to add all the fields from the database that you want to list)

双击源:ADOQuery1并将所需的所有字段添加到可用字段列表中。然后添加一个新字段。 (我已将其他字段留空,因为我很懒,但您应该确保添加要列出的数据库中的所有字段)

如何在DBGrid中显示与数据库中的值不同的值?

You set the properties as required (don't forget that radiobutton in the middle of the dialog).

您可以根据需要设置属性(不要忘记对话框中间的radiobutton)。

In your form a new Field will be added matching the name you gave in the Name box (prefixed with the dataset name).
You then double click on the OnCalcFields event of the dataset (ADOQuery1) and insert to code to populate your calculated fields, e.g.:

在您的表单中,将添加一个与您在名称框中给出的名称相匹配的新字段(前缀为数据集名称)。然后双击数据集的OnCalcFields事件(ADOQuery1)并插入代码以填充计算的字段,例如:

procedure TForm44.ADOQuery1CalcFields(DataSet: TDataSet);
begin
  ADOQuery1ExampleCalc1.AsString:= 'Prefix:'+DataSet.FieldByName('Field1').AsString;
  ADOQuery1ExampleCalc2.AsInteger:= DataSet.FieldByName('Amount').AsInteger+100;
end;

Note
If you display many rows, you will find that it may display slowly. In that case replace the FieldByName with the actual field reference, e.g. ADOQuery1Field1.
FieldByName does a lookup every time it's invoked which slows things down.

注意如果显示多行,您会发现它可能显示缓慢。在这种情况下,将FieldByName替换为实际的字段引用,例如ADOQuery1Field1。 FieldByName每次调用时都会执行查找,从而减慢速度。

Note 2
You can also combine data from 2 database tables in the OnCalcFields event, but beware that event gets called once for every row on display, so make sure your lookup is snappy.
If not it may be a better idea to change the SQL statement in your query.

注意2您还可以在OnCalcFields事件中组合来自2个数据库表的数据,但要注意为显示的每一行调用一次事件,因此请确保您的查找是快速的。如果不是,那么更改查询中的SQL语句可能是个更好的主意。

Further reading
See here (if you want to do this at run time): Adding a calculated field to a Query at run time

进一步阅读请参见此处(如果要在运行时执行此操作):在运行时向Query添加计算字段

Here's the official documentation: http://docwiki.embarcadero.com/RADStudio/Seattle/en/Defining_a_Calculated_Field

这是官方文档:http://docwiki.embarcadero.com/RADStudio/Seattle/en/Defining_a_Calculated_Field

Sample code: http://docwiki.embarcadero.com/RADStudio/Seattle/en/Programming_a_Calculated_Field

示例代码:http://docwiki.embarcadero.com/RADStudio/Seattle/en/Programming_a_Calculated_Field

#1


4  

To show values in a DBGrid cell that are not actually in your database, you can add a calculated field.

若要在DBGrid单元格中显示实际上不在数据库中的值,可以添加计算字段。

You double click on the source: ADOQuery1 and add all the fields you want to the available field list. Then you add a new field.
(I've left the other fields empty because I'm lazy, but you should make sure to add all the fields from the database that you want to list)

双击源:ADOQuery1并将所需的所有字段添加到可用字段列表中。然后添加一个新字段。 (我已将其他字段留空,因为我很懒,但您应该确保添加要列出的数据库中的所有字段)

如何在DBGrid中显示与数据库中的值不同的值?

You set the properties as required (don't forget that radiobutton in the middle of the dialog).

您可以根据需要设置属性(不要忘记对话框中间的radiobutton)。

In your form a new Field will be added matching the name you gave in the Name box (prefixed with the dataset name).
You then double click on the OnCalcFields event of the dataset (ADOQuery1) and insert to code to populate your calculated fields, e.g.:

在您的表单中,将添加一个与您在名称框中给出的名称相匹配的新字段(前缀为数据集名称)。然后双击数据集的OnCalcFields事件(ADOQuery1)并插入代码以填充计算的字段,例如:

procedure TForm44.ADOQuery1CalcFields(DataSet: TDataSet);
begin
  ADOQuery1ExampleCalc1.AsString:= 'Prefix:'+DataSet.FieldByName('Field1').AsString;
  ADOQuery1ExampleCalc2.AsInteger:= DataSet.FieldByName('Amount').AsInteger+100;
end;

Note
If you display many rows, you will find that it may display slowly. In that case replace the FieldByName with the actual field reference, e.g. ADOQuery1Field1.
FieldByName does a lookup every time it's invoked which slows things down.

注意如果显示多行,您会发现它可能显示缓慢。在这种情况下,将FieldByName替换为实际的字段引用,例如ADOQuery1Field1。 FieldByName每次调用时都会执行查找,从而减慢速度。

Note 2
You can also combine data from 2 database tables in the OnCalcFields event, but beware that event gets called once for every row on display, so make sure your lookup is snappy.
If not it may be a better idea to change the SQL statement in your query.

注意2您还可以在OnCalcFields事件中组合来自2个数据库表的数据,但要注意为显示的每一行调用一次事件,因此请确保您的查找是快速的。如果不是,那么更改查询中的SQL语句可能是个更好的主意。

Further reading
See here (if you want to do this at run time): Adding a calculated field to a Query at run time

进一步阅读请参见此处(如果要在运行时执行此操作):在运行时向Query添加计算字段

Here's the official documentation: http://docwiki.embarcadero.com/RADStudio/Seattle/en/Defining_a_Calculated_Field

这是官方文档:http://docwiki.embarcadero.com/RADStudio/Seattle/en/Defining_a_Calculated_Field

Sample code: http://docwiki.embarcadero.com/RADStudio/Seattle/en/Programming_a_Calculated_Field

示例代码:http://docwiki.embarcadero.com/RADStudio/Seattle/en/Programming_a_Calculated_Field