Delphi grid with different datatype on each line, rendered dynamically

I am trying to create a Delphi grid to allow displaying and editing in the grid of db data that may have a different datatype for each row. I would like to display a specific control for each data type, eg. when the datatype is DateTime, I want to display my own edit control that allows you to enter a date or call the calendar.

The data looks something like this:

Name   DataType   DateValue   StringValue   BooleanValue     
---------------------------------------------------------
A      Date       1/1/2007 
B      String                 asdf
C      Boolean                              True

      

... and in db this table has a column for every possible value type. So there is a column BooleanValue

, DateValue

etc.

What I would like to do is display a single "Value" column in the table, which displays the appropriate edit control depending on what the "DataType" is for that row. So the grid should look like this:

Name   DataType   Value     
---------------------------
A      Date       1/1/2007 
B      String     asdf
C      Boolean    True

      

It seems I will need to display another edit control (to allow the user to edit the column Value

) for each row dynamically based on the column value DataType

. I know there are more sophisticated networks out there that handle this kind of problem, but the permissions that will will not allow anything other than what's available out of the box with Delphi.

Any ideas on how to make something like this work?

+3


source to share


3 answers


Personally, I wouldn't go for editing directly internally TDBGrid

in this case, since your table is not DB normalized (I don't use it at all anyway). I would use a Calculated field to display the desired value in a grid and dynamically create TDBxxxEdit

in the form for each type of field (for example about your own TDBTreeEdit

, a TDBRichEdit

or a DB image editor pickups, etc?).



If you want to use your own controls in TDBGrid

and replace the default editor TInplaceEdit

, you can refer to the following article: Adding Components to a DBGrid and related article: Displaying and Editing MEMO Fields in Delphi TDBGrid

+4


source


Displaying all data in one column is pretty straightforward. You can simply add the computed string field and change the value according to what you store in that string.

Editing is a bit tricky. If you want to have an editor in place, you are in a world of resentment ... I did it, it is painful and takes a long time. If you want to display a dialog to edit the value, it is much easier. You can add column objects to the grid, and you can customize the column that you connected to the calc field to display the button. When the button is clicked, you simply show the edit dialog required for that line, and commit the changes when the dialog is closed.



There are other ways to do this, but I would say that this would be the shortest way. Other ways might include custom paint events to display your data in a single column, catch clicks to create your own editor, etc. Etc. Etc.

+3


source


after adding the calculated fields.
Example:

procedure OnCalculate(DataSet:TDataSet);
begin
  case IndexText(DataSet['ValueType'],['Date','String','Boolean']) of 

    0:DataSet['DateValue']:=StrToDateTime(DataSet['Value']); // also converting
    1:DataSet['StringValue']:=DataSet['Value'];
    2:DataSet['BooleanValue']:= MatchText(DataSet['Value'],['1','True','T','Y','Yes']);
{etc datatypes}
   end;
end;

      

0


source







All Articles