3

Is there any real equivalent of C#'s DataGridView in Delphi?

I have tried

  • TStringGrid

    • But the scrollbar is either invisible when all the items are visible, or it is the smallest scrollbar possible no matter how little the items are overflowed and only updates when the scrollbar is released, not when it is being dragged
    • Also, if you have one fixed row, you have to have at least one (empty) row besides that, which is inconvenient and unsightly
  • TDBGrid

    • But I can't seem to figure out how to add items programmatically (and I don't think it's meant to do that anyway). If I could do that, TDBGrid would be fine for me to use.

So what is the Delphi equivalent of C#'s DataGridView that doesn't have the problems listed above?

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • Just link a TDbGrid to a TTable or other TDataSet. – H H Dec 21 '11 at 22:28
  • @HenkHolterman I will try that now and see if I can make it work – Seth Carnegie Dec 21 '11 at 22:29
  • if you can't make it work.. paste your code.. I have been doing Delphi for 17 yrs so I know both Delphi and C# hope @Henk comments helped he's right on point also hope you have a valid datasource component as well perhaps you have all the right components but are not wiring them up properly.. I would stay away from TStringGrid reguires too much manually coding if you want to do any custom style stuff.. also look at TClientDataSet works wonders too – MethodMan Dec 21 '11 at 22:33
  • @HenkHolterman @DJ KRAZE I tried doing both `TDbGrid1.DataSource := ClientDataSet1;` and `TDbGrid1.DataSource := TTable1;` but it can't compile because the types are wrong. Sorry, I'm a little new to delphi so I may be missing something obvious – Seth Carnegie Dec 21 '11 at 22:40
  • @SethCarnegie to you must drop a tdbgrid, tdatasource and tdataset (descendent) in the form and then connect in this way tdbgrid.datasource:=datasource , datasource.dataset:=clientdataset – RRUZ Dec 21 '11 at 22:45
  • @RRUZ Yes I had just figured that much out, how does one add a line to the grid then? – Seth Carnegie Dec 21 '11 at 22:47
  • You must call the method append of the clientdataset1 , then set the values of each field and finally call the post method. – RRUZ Dec 21 '11 at 22:56
  • @RRUZ thanks, I don't know how to set the values of each field, and also when I try to call `Append` it says "cannot perform this operation on a closed dataset". When you finish telling me how, please post that info as an answer and I'll accept it. – Seth Carnegie Dec 21 '11 at 23:07

2 Answers2

3

The DataGridView is a very flexible control which can work in bound and unbound modes, in the Delphi side you must choose bewteen 2 kinds of controls, for example if the content of the control can be edited directly (unbound) you can choose a component like a TStringGrid or in bound mode which in delphi is called data-aware you must choose something like a dbgrid, in this last case you edit the dataset asociated to the control and the control reflect the content of the dataset. Using this last scenario. you have several options about the dataset component to choose maybe the most flexible is the TClientDataSet. if you want learn more about this topic check these links

Note : in the last version of Delphi (XE2) a new concept was introduced called LiveBindings, which introduces big changes in how you can bind a object or component to a collection or another component.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • I'm sorry I'm still having trouble but I can't set the `Active` property of my `TClientDataSet` to `true` because it says "Missing data provider or data packet". Is there any examples of this that I can look at? – Seth Carnegie Dec 21 '11 at 23:37
  • TClientDataset either serves as a stand-alone in-memory dataset, or as a bridge between a datasource and another dataset, which can either be local or remote. If you Open a ClientDataset, it must be connected to a provider, which must be connected to a dataset like a TADOQuery. You can also define fields yourself and call CreateDataset to make the clientdataset function as a stand-alone dataset. TClientDataset is very flexible, but you don't necessarily need it when you want to show the results of a query in a grid. Just the query (=dataset), datasource and the grid are enough. – GolezTrol Dec 21 '11 at 23:56
  • @GolezTrol I don't want it to show the results of a query, I just want to show some strings in a table-like layout. I still can't figure out how to insert a new row... I'm really sorry, this just escapes me. – Seth Carnegie Dec 22 '11 at 00:08
  • Ah, I misunderstood. In that case, you can either pick a TStringGrid, which allows you to add rows by settings the RowCount and ColumnCount properties. Then, you can access any cell to add strings. If the data is structured like a table (columns of specific types), you can make use of the ClientDataset and use it as an in-memory dataset. You add the data to the ClientDataset and use TDBGrid to display it. – GolezTrol Dec 22 '11 at 00:29
  • I use clientdatasets quite a lot for display and input of this kind, because the DBAware controls (TDBGrid, TDBEdit, etc) are aware of how to edit and display certain types of field. It saves you a lot of validation, if you hook up a DBEdit to a ClientDataset with an integer field. But that's a different story, and I don't know if everyone would aprove of this method. :) – GolezTrol Dec 22 '11 at 00:31
  • @GolezTrol make sure you do @SethCarnegie or else I am not alerted of your replies. But, I tried using `TStringGrid` and don't like it because of the scrollbar issues. What I need is something with the behaviour of `TStringGrid` with the look of `TDBGrid` – Seth Carnegie Dec 22 '11 at 01:28
  • @SethCarnegie Thank you for reminding me. I sometimes forget. – GolezTrol Dec 22 '11 at 20:52
0

I would recommend you to check DevExpress.com QuantumGrid - it works both in Bound and Unvound mode

Gad D Lord
  • 6,620
  • 12
  • 60
  • 106