3

I am trying to create a Delphi grid to allow display and edit in a db grid of data that might have a different data type on each row. I would like to display a specific control for each data type, e.g. when the data type is DateTime, I want to display my custom edit control that allows typing a date in or popping up a 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 the db, this table has a column for each possible type of value. So, there is a BooleanValue column, DateValue, etc.

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

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

It seems I will need to display a different edit control (to allow the user to edit the Value column) for each row dynamically based on the value of the DataType column. I know there are more advanced grids out there that handle this sort of problem, but the powers that be will not allow anything but what is available out-of-the-box with Delphi.

Any ideas on how to make something like this work?

LU RD
  • 34,438
  • 5
  • 88
  • 296
  • It sounds like you're going to be overloading your grid's "OnDraw" event – paulsm4 Apr 09 '12 at 18:47
  • 2
    Trying to find an out of the box solution or a 3rd party control for this is pointless. What you're telling does not fit into a data table concept. – Sertac Akyuz Apr 09 '12 at 18:50
  • What type are the fields in the database? String? – David Heffernan Apr 09 '12 at 19:29
  • In the database, DateValue is a DateTime, StringValue is a String, BooleanValue is a BIT, etc. – John Marquez Apr 09 '12 at 19:32
  • greatis has a set of inspector components, including a TDBInspector. It may do what you need but isn't free: http://www.greatis.com/delphicb/objinsp/ – Marjan Venema Apr 09 '12 at 20:00
  • The Powers That Be should go ahead and implement it themselves then. This is Delphi, and in Delphi, you write your own controls, and you use controls other people wrote. Anybody who says you shouldn't is a moron. I could write a perfectly workable implementation of your question in a custom control using my "virtual grid" control in about 2 hours. But I won't bother even posting an answer unless the "no external controls" requirement goes away, because that's just dumb. – Warren P Apr 09 '12 at 23:40

3 Answers3

4

Personally, I would not go for editing directly inside the TDBGrid in this case, since your Table is not DB normalized (I don't use it in any case actually). I would have used a Calculated field to display the desired value in the grid, And dynamically created the TDBxxxEdits on the form for each field type (How about your own TDBTreeEdit for example, a TDBRichEdit, or a DB Image pickup editor, etc...?).

In case you do want to use your own controls on the TDBGrid, and replace the default TInplaceEdit editor, you can refer the following article: Adding components to a DBGrid, and a related article: Displaying and editing MEMO fiels in Delphi's TDBGrid

kobik
  • 21,001
  • 4
  • 61
  • 121
3

Displaying all of the data in the same column is quite easy. You can simply add a calculated string field, and change the value according to what you are storing in that row.

The editing is quite a bit more complicated. If you want to have an in-place editor, you are in for a world of hurt... I've done it, it's a pain, and takes a lot of time. If you want to display a dialog to edit the value, that's much easier. You can add a column objects to the grid and you can setup the column you have attached to the calc field to display a button. When the button is clicked you simply display the editing dialog needed for that row, and commit the edits when the dialog is closed.

There are other ways to get this done, but I would say the above would be the shortest way. Other ways may include custom draw events to display your data in one column, intercept clicks to create your own editor, etc, etc, etc...

GDF
  • 830
  • 5
  • 11
0

after add calculated fields .
Sample:

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;
MajidTaheri
  • 3,813
  • 6
  • 28
  • 46