Insert column into string grid, delphi - delphi

I have a string grid, from which i can delete columns. I defined a CustomStringGrid type that allows me to use DeleteColumn method.
This is how it looks:
TCustomStringGrid = class(TStringGrid)
[...]
With tCustomStringGrid(mygrid) do
DeleteColumn(col)
end;
IS there something similar to add a column? I've tried InsertColumn but it doesn't seem to exist. I want to add a column at a particular position. In fact, if a user deletes a column i have an undo button which i want to reinsert the deleted column (i'm keeping the data in an array so i can recreate the column but i don't know how to insert one in a particular position).
Thank you!

It's not built in but easy to emulate, with ColCount = ColCount + 1 and MoveColumn from a HackClass.
type
THackGrid=Class(Grids.TCustomGrid)
End;
Procedure InsertColumn(G:TStringGrid;Position:Integer);
begin
if Position<G.ColCount then
begin
G.ColCount := G.ColCount + 1;
THackGrid(g).MoveColumn(G.ColCount - 1,Position);
end;
end;
procedure TMyForm.Button1Click(Sender: TObject);
begin
InsertColumn(StringGrid1,1);
end;

THack grid is not working, maybe it is ok when both cols are visible, but that works always :
Procedure MoveColumn(G:TStringGrid;OldPosition : integer;NewPosition:Integer);
var
i : integer;
temp : string;
begin
for i := 0 to g.rowcount - 1 do
begin
temp := g.cells[OldPosition,i];
g.cells[OldPosition,i] := g.cells[NewPosition,i];
g.cells[NewPosition,i] := temp;
end;
end;

Related

How To Increment Numbers in Calculated field of TQuery?

I Have TQuery With Calculated Field N.
How To Increment Numbers in the example (N starts with 5):
I tried this but Nothing:
procedure TForm1.Query1CalcFields(DataSet: TDataSet);
var i:integer;
begin
i := strtoint(edit2.Text);
Query1['N'] := inttostr(i+1);
end;
result:
N
2
2
2
2
.
.
Note: Foxpro database ,i use BDE to connect with ,It does not have to be a calculated field ,i want the Incremented value to use it in print of quickreport like a single reference for each Page (not pagenumber).
I Found This Solution With The Help Of #kobik
In Printing Of TQRLabel I Add This Code And No Needed To The Calculated Field Or Other Varible:
procedure TForm1.QRLabel1Print(sender: TObject; var Value: string);
begin
value:=inttostr(Query1.RecNo+strtoint(edit2.Text)-1);
end;
The Tedit To Costume The Start Number At Runtime.
This is a simple way that I test it:
1- declare a global variable for saving auto number
2- Set it in FormShow to 5
3- In OnCalcFields assign global variable to the new field
4- increment global variable
Notes: Do not use TEdit or any thing for show the result of calculate field, because it will just show the first result. but all the result will save in table or query correctly.
Codes
Global Variable:
var
Form1: TForm1;
i : Integer;
Form Show:
procedure TForm1.FormShow(Sender: TObject);
begin
i := 5;
end;
Calc Filed:
procedure TForm1.adoqry1CalcFields(DataSet: TDataSet);
begin
adoqry1['n'] := i;
//OR adoqry1N.AsInteger := i;
//OR adoqry1.FieldByName('n').AsInteger := i;
i := i + 1;
end;
At the end, I test it with ADOQuery.

cxGrid - Footer summary of checked records

In cxGrid,I have a column that is boolean (properties : checkbox).
How can I do a footer summary (SUM) of such a column i.e to sum how many records are checked.
Right now, if I set it to SUM, my footer summary displays negative numbers for the items checked.How can I avoid these negative numbers?
edit :
I have found a would be solution on their site with :
procedure TForm1.cxGrid1DBTableView1DataControllerSummaryFooterSummaryItemsSummary(
ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments;
var OutArguments: TcxSummaryEventOutArguments);
var
si: TcxGridDBTableSummaryItem;
begin
si := Arguments.SummaryItem as TcxGridDBTableSummaryItem;
if si.Column = cxGrid1DBTableView1Sonda then
OutArguments.Done := not OutArguments.Value;
end;
However I am getting the error :
Could not convert variant of type (Null) into type (Boolean).
Dont understand this. Field is boolean type (bit).
edit2:
The problem is that sql server by default sets boolean type to NULL.
That is why the conversion error.
You can also just set your grid to calculate that summary using a different field , for example a calculated field where you assign the exact value you want to add each time.
Add a calculated field to your dataset, with the desired value.
MyHiddenField.Value := -1 * YourCheckingField.AsInteger;
Go to the Summaries Tab on the CxGrid dialog, and add a new Summary:
Set the Column property to the Grid Column where you want it to appear
Set the FieldName to your calculated field
And finally set Kind to skSum
It is better to send such questions to DevExpress support team.
You can customize footer:
assign Kind=skNone to footer summary item
use OnGetText event to show what you want
Quick example (shows number of chars in all records as footer value):
procedure TForm54.cxGrid1DBTableView1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems0GetText(
Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean;
var AText: string);
var i,j: integer;
begin
j := 0;
for i := 0 to cxGrid1DBTableView1.DataController.RecordCount-1 do
j := j + Length(String(cxGrid1DBTableView1.DataController.Values[i, cxGrid1DBTableView1c.Index]));
AText := IntToStr(j);
end;
I think that you can resolve that problem in SQL component. Use typecasting and your cxGrid will work with Integer values.
SELECT CAS(Bit_Column AS int) AS Int_Column
FROM YourTable
You can try this :
procedure TForm1.cxGrid1DBTableView1DataControllerSummaryAfterSummary(
ASender: TcxDataSummary);
var i, j, Imp:integer;
Item: TcxGridDBTableSummaryItem;
begin
DBTableView1.DataController.BeginLocate;
for j:=0 to ASender.FooterSummaryItems.Count-1 do ASender.FooterSummaryValues[j]:=0;
try
for i:=0 to DBTableView1.DataController.RowCount-1 do
begin
if (DBTableView1.DataController.Values[i,cxGrid1DBTableView1Sonda.Index]<>null) then
for j:=0 to ASender.FooterSummaryItems.Count-1 do
begin
Item:=TcxGridDBTableSummaryItem(ASender.FooterSummaryItems[j]);
Imp:= DBTableView1.DataController.Values[i, cxGrid1DBTableView1Sonda.Index];
if (Imp= 1) or ((Imp= 2)
then ASender.FooterSummaryValues[j]:= ASender.FooterSummaryValues[j]+1;
end;
end;
finally
DBTableView1.DataController.EndLocate;
end;
end;
procedure cxGrid1DBTableView1DataControllerSummaryFooterSummaryItemsSummary(
ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments;
var OutArguments: TcxSummaryEventOutArguments);
var
AValue: Variant;
AItem: TcxGridTableSummaryItem;
begin
AItem := TcxGridTableSummaryItem(Arguments.SummaryItem);
// считаем проверенные
if (AItem.Column = tvCompareisCorrect) and (AItem.Kind = skCount) and (AItem.Position = spFooter) then begin
AValue := tvCompare.DataController.Values[ Arguments.RecordIndex, tvCompareisCorrect.Index];
if not VarIsNull(AValue) then
if not VarAsType(AValue, varBoolean) then Dec(OutArguments.CountValue);
end;

Load database field of all records into ListView Item Detail Object

Using Delphi XE8 I'm currently testing functionality with Firemonkey TListViews.
One thing I'm trying to do is to load a field of all records from a TFDMemtable component into a Listview Item, specifically into the DetailObject of the ListView Item.
For example, I have 3 records in a table (db field is called 'Name'):
Record 1 = Name 1
Record 2 = Name 2
Record 3 = Name 3
There is only 1 DetailObject property per ListView Item so my question is, would I be able to add all of the fields (Name 1, Name 2, Name 3) into that one DetailObject?
Below is what I've attempted so far but no luck. Not 100% sure what I need to do.
procedure MainForm.BuildList;
var LItem : TListViewItem;
begin
ListView1.BeginUpdate;
try
ListView1.CLearItems;
LItem := ListView1.Items.Add;
LItem.Objects.DetailObject.Visible := True;
with memtable do
begin
while not eof do
begin
LItem.Detail := FieldByName('Name').AsString;
end;
end;
finally
ListView1.EndUpdate;
end;
end;
I'm sorry if this isn't clear enough, please let me know.
Any help would be great.
I think I should warn you that before seeing your q, I'd never done anything with FMX ListViews and Master/Detail datasets. The Following is a little rough around the edges, and the layout isn't ideal, but it shows one way to populate a ListView from Master + Detail datasets. I have no idea whether there are better ways. Personally, I would see if I could use Live Bindings to do the job.
procedure TMasterDetailForm.BuildList;
var
LItem : TListViewItem;
DetailItem : TListViewItem;
ListItemText : TListItemText;
DetailIndex : Integer;
begin
ListView1.BeginUpdate;
ListView1.ItemAppearanceObjects.ItemEditObjects.Text.TextVertAlign := TTextAlign.Leading; // The default
// seems to be `Center`, whereas we want the Master field name to be at the top of the item
try
ListView1.Items.Clear; //Items;
Master.First;
while not Master.eof do begin
LItem := ListView1.Items.Add;
LItem.Text := Master.FieldByName('Name').AsString;
LItem.Height := 25;
Detail.First;
DetailIndex := 0;
while not Detail.Eof do begin
Inc(DetailIndex);
ListItemText := TListItemText.Create(LItem);
ListItemText.PlaceOffset.X := 100;
ListItemText.PlaceOffset.Y := 25 * (DetailIndex - 1);
ListItemText.TextAlign := TTextAlign.Leading;
ListItemText.Name := 'Name' + IntToStr(DetailIndex); //Detail.FieldByName('Name').AsString;
LItem.Data['Name' + IntToStr(DetailIndex)] := Detail.FieldByName('Name').AsString;
Detail.Next;
end;
LItem.Height := LItem.Height * (1 + DetailIndex);
Master.Next;
end;
finally
ListView1.EndUpdate;
end;
end;
TListItemText is one of a number of "drawable" FMX objects that can be added to do the TListViewItem. They seem to need unique names so that they can be accessed via the Names property.
FWIW, I used 2 TClientDataSets as the Master and Detail in my code.
Also FWIW, for FMX newbies like me, populating an FMX TreeView is a lot more like what you'd do in a VCL project:
procedure TMasterDetailForm.BuildTree;
var
PNode,
ChildNode : TTreeViewItem;
begin
TreeView1.BeginUpdate;
try
TreeView1.Clear;
Master.First;
while not Master.eof do begin
PNode := TTreeViewItem.Create(TreeView1);
TreeView1.AddObject(PNode);
PNode.Text := Master.FieldByName('Name').AsString;
Detail.First;
while not Detail.Eof do begin
ChildNode := TTreeViewItem.Create(TreeView1);
ChildNode.Text := Detail.FieldByName('Name').AsString;
PNode.AddObject(ChildNode);
Detail.Next;
end;
Master.Next;
end;
finally
TreeView1.EndUpdate;
end;
end;
Btw, in your code you should have been calling
memtable.Next;
in your while not eof loop, and memtable.First immediately before the loop.

How to delete selected records from TDBAdvGrid?

I want to delete multiple selected records which I have displayed at TDBAdvGrid. A number of records are being selected by checking checkbox in front of them. After clicking on Delete button it triggers procedure as follows. It is returning values of selected rows id successfully only problem with deletion of all records. It deletes only first selected record.
procedure TForm5.Button3Click(Sender: TObject);
var
i,j,idn: Integer;
State: Boolean;
begin
j := 0;
for i := 1 to DBAdvGrid1.RowCount - 1 do
begin
if DBAdvGrid1.GetCheckBoxState(1,i,state) then
begin
if state then
begin
idn := StrToInt(DBAdvGrid1.Cells[6,i]);
UniQuery1.SQL.Text := 'Delete from userplays where id = :id';
UniQuery1.ParamByName('id').AsInteger := idn;
UniQuery1.ExecSQL;
end;
end;
end;
end;
It is deleting only first record in lineup. After deleting first record it breaks for loop and control goes back to TDBAdvGrid with updated data after delete.
Final code based on suggestion is as follows
j := 0;
DBAdvGrid1.BeginUpdate;
for i := 1 to DBAdvGrid1.RowCount - 1 do
begin
showmessage('inside rowcount loop');
if DBAdvGrid1.GetCheckBoxState(1,i,state) then
begin
if state then
begin
DBAdvGrid1.DataSource.DataSet.First;
DBAdvGrid1.DataSource.DataSet.MoveBy(i - 1 - j);
DBAdvGrid1.DataSource.DataSet.Delete;
j := j+1;
continue;
end;
end;
end;
This code works perfectly only when PageMode := False
Based on the suggestion above, I would like to make another suggestion...
//DBAdvGrid1.BeginUpdate;
query.First;
for i := 1 to DBAdvGrid1.RowCount - 1 do
begin
if DBAdvGrid1.GetCheckBoxState(1,i,state) then
begin
if state then
begin
//you can do whatever you want with the current selected record
end;
end;
query.Next;
end;
I think this is easier then using the MoveBy...
I even never changed PageMode, it's still on true. No idea if it's coincidence or not.
You can use cyBookmarks in the OnCheck event , with Virtual Table.
It creates a list of all records selected, and you can list them with
cyBookmarks.bookmarklits.count, so my example is like this :
procedure TForm2.cyDBGrid1CheckBoxClick(Sender: TObject);
begin
if cyBookmarks1.AddCurrentRecord then
begin
cyBookmarks1.BookmarkList.InsertBookmark(cyDBGrid1.CheckedList.CurrentRecord);
cyDBGrid1.Invalidate ;
end
else
begin
cyBookmarks1.RemoveCurrentRecord;
cyDBGrid1.Invalidate;
end;
if cyBookmarks1.BookmarkList.Count>0 then
begin
VirtualTable1.GotoBookmark(cyBookmarks1.BookmarkList.Items[0]);
lbl1.Caption:=VirtualTable1.FieldByName('N_ENREGISTREMENT').AsString;
end;
end;

to retrieve stringlist items in memo from table POS_CatBreakDownValue into tchecklistbox

i have a problem on how to retrieve the items in memo from table into tchecklistbox...before this i do the procedure to insert the items that have been checked into memo in tbl POS_CatBreakDownValue..but now i want to know how to retrieve items from table and automatic check in tchecklistbox...thanks..here my procedure to insert the checklist into memo in table..i hope anyone can help me..thanks
procedure TfrmSysConfig.saveCatBreakDownValue;
var
lstCat:TStringList;
i :integer;
begin
lstcat := TStringList.Create;
try
for i:=0 to clCat.Items.Count-1 do begin
if clCat.Checked[i] then begin
lstcat.Add(clcat.Items.Strings[i]);
end;
end;
tblMainPOS_CatBreakDownValue.Value := lstCat.Text;
finally
lstcat.Free;
end;
end;
procedure TfrmSysConfig.saveCatBreakDownValue;
var
lstCat: TStringList;
i:integer;
begin
lstcat := TStringList.Create;
try
for i:=0 to clCat.Items.Count-1 do begin
if clCat.Checked[i] then begin
lstcat.Add(clcat.Items.Strings[i]);
end;
end;
tblMainPOS_CatBreakDownValue.Value := lstCat.Text;
finally
lstcat.Free;
end;
end;
Reading your code I'm guessing you've got a MemoField in a database that you are reading and writing the checked values from/to. You also have a predefined list of checkable items.
So you'll need to create a new string list and read the field back into it (Revesing the writing code). for each item in the list get the Index and check it.
Something like..
procedure TfrmSysConfig.saveCatBreakDownValue;
var
lstCat: TStringList;
i, Index:integer;
begin
lstcat := TStringList.Create;
try
lstcat.Text = tblMainPOS_CatBreakDownValue.Value;
for i:=0 to lstcat.Count-1 do
begin
Index := clCat.Items.IndexOf(lstcat.Items[i])
if Index > -1 then
begin
clCat.Checked[Index] := True;
end;
end;
finally
lstcat.Free;
end;
end;
Not sure I understand your question 100%
TCheckListBox can check or uncheck items using the Checked Property.
CheckListBox.Checked[Index] := True/False;
Since you sound like your comparing strings you may need to determine the index in the TCheckListBox based on the string this can be done like this:
CheckListBox1.Items.IndexOf('StringToFind')
If the string is not found then the result is -1;
If you want to check lines in a TMemo Control and see if they exists as rows in a table you can do the following.
While not Table.EOF do
begin
if Memo1.lines.IndexOf(Table.FieldByName('MyField').AsString) = -1 then
begin
// What you want to do if not found
end
else
begin
// what you want to do if it is found.
end;
Table.Next;
end;

Resources