cxGrid: how to cast a cell to a combobox-object - delphi

i have a problem with my cxgrid. in my cxgrid there a different rows and columns which have comboboxes as properties. but how can i cast a special cell value to a combobox-object? for example: i want to set the itemindex of the combobox in row 1 and column 2 to 0.
thanks!

The ItemIndex you cannot set, because it is suplied by the DataSource (or data provider).
But you can set other properties implementing the event OnGetProperties of your grid column:
TcxComboBoxProperties(Sender.Properties).DropDownRows := 9

Related

How to change DBgrid row value?

I would like to change DBgrid row value with outside data, how to achieve that?
For example i would like to add Tedit value into dbgrid selected row column(for example 5) via button click.
Also, i would like to add value from 1 dbgrid to another dbgrid.(Add to an existing number, not replace).
Suppose you have a dataset connected to db-aware controls like TDBEdit and TDBGrid via a TDataSource. Delphi's db-aware controls are basically the default ones which come with Delphi and are displayed on the Data controls tab of its Component Palette plus any 3rd-party ones that you install. The reason they are called db-aware is because they are written so that the values they display are automatically derived from the related fields of the dataset.
Also suppose that the dataset is called Table1 and has a CustomerName field that you want to change. The simplest code which will achieve that is something like:
Table1.Edit; // put table one into dsEdit state so that field values can be changes
Table1.FieldByName('CustomerName').AsString := 'Jones';
Table1.Post; // save the change(s) to Table1
More optimal code might be
Table1.Edit;
try
Table1.DisableControls; // this prevents the db-aware controls updating on-screen while the changes are made
Table1.FieldByName('CustomerName').AsString := 'Jones';
Table1.FieldByName('CustomeCountry').AsString := 'DE#;
finally
Table1.Post;
Table1.EnableControls; // Eable screen updating od the db-aware controls again
end;
That would update the values displayed in DBEdit controls linked to the CustomerName and CustomerCountry fields and in the CustomerName and CustomerCountry cells of the current (i.e. highlighted) row in the DBGrid, because the current row in the DBGrid always tracks the current row in the connected dataset.
If you really do want to update a given column from a TEdit's text you could do that like this:
Table1.Edit;
DBGrid1.Columns[5].Field.AsString := Edit1.Text;
Table1.Post;

Delphi gridpanel check if cell is empty

I want to check if a cell in a gridpanel is empty or not by using the row and column value of that cell.
Any ideas?
You can test if a specific cell has a control or not by querying the control item of that cell in the ControlCollection collection.
if Assigned(MyGridPanel.ControlCollection.ControlItems[x, y]) and
Assigned(MyGridPanel.ControlCollection.ControlItems[x, y].Control) then

Selecting a row in TStringGrid from click on any cell within that row.

I have a TStringGrid object on a form which has 1 FixedCol and 1 FixedRow. I want to be able to select an entire row on the object when the user clicks on any cell within that row. This selection must also be visible to the user (I want the row to change colour).
EDIT: Have put goRowSelect in options on the object. Is there now a way to select the row on the click of one of the cells in the fixed column?
In order highlight entire row when ever the user clicks on any cell in that row, set the following StringGrid properties.
In design time: Go to options property and check the following sub-properties.
goEditing := TRUE;
goRowSelect := TRUE;
To achieve this at run time,
StringGrid1.Options := StringGrid1.Options + [goEditing];
StringGrid1.Options := StringGrid1.Options + [goRowSelect];
If you enable the option goFixedColClick of the string grid you can then use the OnFixedCellClick event to determine which row to select. You can then set StringGrid1.Selection := TGridRect(Rect(0, Row, n, Row)); where Row is the row clicked and n is the width of your StringGrid.

What dataset row appears in the top-most row of DBGrid when RowCount > VisibleRowCount without that first row of the grid being selected?

Is it possible to get the row number that is displayed from the underlying dataset in the top-most row of a DBGrid, without that top-most row being the currently selected row, when the number of records in the underlying dataset is greater than the number of rows displayed in the DBGrid, and the DBGrid has been scrolled.
Here's my problem. From a drag/drop event handler attached to the DBGrid I can determine which visible row of the DBGrid the drop event is associated with using MyGrid.MouseCoord(X,Y).Y. When the underlying dataset contains less than or the same number of records as the number of rows displayed in the DBGrid, this value is also the row number of the associated record in the underlying dataset.
When the underlying dataset contains more records than the number of visible rows in the DBGrid, MyGrid.MouseCoord(X, Y).Y and TDataSet(MyGrid.DataSource.DataSet).RecNo are the same only when the first row of the dataset appears on the first row of the grid.
Is there some way to identify the record number in the underlying dataset (or the offset) of the top-most displayed record in a DBGrid without that DBGrid row being selected? I know that if I actually select the top-most row of the DBGrid then I can use TDataSet(MyGrid.DataSource.DataSet).RecNo to get the current record number of the underlying dataset. However, from DBGrid.OnDragOver or DBGrid.OnDragDrop events I only have a reference to the DBGrid and the mouse coordinates (from which I can determine which row of the grid was the target of the drop).
For example, if I can determine that the DBGrid is displaying the third record in the underlying dataset in the top-most row of the grid, my problem is solved. Likewise, if I can read an underlying TField of a particular row (say, the top-most row) without that row being select, I have what I need. However, I cannot see a way to do this.
Any suggestions will be greatly appreciated.
Edit: I previously posted a blog about dragging and dropping into a DBGrid. With this new info, I can solve a previously known problem. I will be updating that blog sometime this week, and will add a link to that blog here once I have done so.
There is an additional issue. When the number of visible rows is less than the number of underlying records, we need to also accomdate calculating the drop when it occurs after the last visible row. When dropping after the last visible row, MouseCoord(x,y).Y returns -1.
Here is a modification to Uwe's code that accomplishes that end:
function TDBGridHelper.RecNoFromVisibleRow(Value: Integer): Integer;
begin
if Value = -1 then
begin
Result := DataSource.DataSet.RecNo - Row + TopRow + VisibleRowCount
end
else
begin
Result := DataSource.DataSet.RecNo - Row + TopRow + Value;
if dgTitles in Options then
Dec(Result);
end;
end;
Edit: As I mentioned in the original question, I was interested in this answer in order to fix a behavior in my code that implements drag and drop into a DBGRid. With this answer I have updated my drag and drop behavior, and I have written about this update in my blog. You can find this discussion, including links to the original blog post, at the following URL: Dragging and Dropping into a DBGrid Revisited
As long as there is only an offset between DataSet.RecNo and the visible rownumber in the grid, you might get the required information from the protected members Row and TopRow, which can be accessed by a class helper. Something like this:
function TDbGridHelper.RecNoFromVisibleRow(Value: Integer): Integer;
begin
Result := DataSource.DataSet.RecNo - Row + TopRow + Value;
if dgTitles in Options then
Dec(Result);
end;

delphi comboBox

how does one get the picked value of a combo box from delphi 7
lets say it has items, how does one know which row (item) was picked?
You find the selected item with:
combobox.ItemIndex; // -1 if none
You can get the value using
combobox.Items[combobox.ItemIndex]
combobox.text will hold the value which was selected
When you want to use combobox.text, i recommend to set style to csDropDownList and ItemIndex to 0 in object inspector to have combobox initialized.

Resources