i dont know how to deal with stringrids, i want to fill it with data, I've succeeded to do it with a listview
this is my code to fill the listview..
var
LJSONArray : TJSONArray;
LItem: TListViewItem;
I: Integer;
begin
LJSONArray := TJSONArray.Create;
try
BackendStorage1.Storage.QueryObjects('ShoppingList', [], LJSONArray);
ListView1.ClearItems;
for I := 0 to LJSONArray.Count-1 do
begin
LItem := ListView1.Items.Add;
LItem.Text := (LJSonArray.Items[I].GetValue<string>('item'));
end;
finally
LJSONArray.Free;
end;
end;
To add items to a TStringGrid, you have to set its RowCount property first and then use its Cells property to fill them in, eg:
var
LJSONArray : TJSONArray;
I: Integer;
begin
LJSONArray := TJSONArray.Create;
try
BackendStorage1.Storage.QueryObjects('ShoppingList', [], LJSONArray);
StringGrid1.RowCount := LJSONArray.Count;
for I := 0 to LJSONArray.Count-1 do
begin
StringGrid1.Cells[0, I] := LJSonArray.Items[I].GetValue<string>('item');
end;
finally
LJSONArray.Free;
end;
end;
Make sure you have set the grid's ColumnCount property to at least 1 beforehand, such as at design-time.
Related
I have dataset with 2 different sources - some are for goverment and others are for organization only. I need to distinct them somehow, like making some items bold or different color.
I tried to use DrawItem event, but couldn't figure it out.
For adding items I used:
while not (cdDataset1.Eof) do
begin
if ((cdDataset1.fieldbyName('displayName').value<> '') and (cdDataset1.fieldbyName('TyypId').value=1280781)) then
begin
cxDBCheckListBox1.Items.Add.Text:= cdDataset1.fieldbyName('displayName').value;
end;
cdDataset1.Next;
end;
cdDataset1.First;
while not (cdDataset1.Eof) do
begin
if ((cdDataset1.fieldbyName('displayName').value<> '') and (cdDataset1.fieldbyName('TyypId').value=1243501)) then
begin
cxDBCheckListBox1.Items.Add.Text:= cdDataset1.fieldbyName('displayName').value;
end;
cdDataset1.Next;
end;
This part works well. But can I use field TyypID for distinction on cxdbchecklistbox ?
It should look like this(checkboxs intead of bullets ofcourse):
Important option1
Impotant option2
extra info option1
I found the solution. First add tags . OnDrawItem creating canvas is needed:
//adding items to checklistbox where needed.
cxDBCheckListBox1.Items.Clear;
while not (cdDataset1.Eof) do
begin
if ((cdDataset1.fieldbyName('displayName').value<> '') and
(cdDataset1.fieldbyName('TyypId').value=28078)) then
begin
with cxDBCheckListBox1.Items.Add do
begin
Text:= cdDataset1.fieldbyName('displayName').value;
Tag := 1280781;
end;
end;
cdDataset1.Next;
end;
//Then on drawitem:
procedure TfmSample.cxDBCheckListBox1DrawItem(
Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
ACanvas: TcxCanvas;
AText: string;
ATextRect: TRect;
AGlyphWidth: Integer;
AListBox: TcxDBCheckListBox;
ACanvasFont: TFont;
AItemEnabled: Boolean;
AItemTag: Integer;
begin
AListBox := (Control as TcxDBCheckListBox);
ACanvas := AListBox.InnerCheckListBox.Canvas;
ACanvasFont := ACanvas.Font;
AItemTag := AListBox.Items[Index].Tag;
AItemEnabled := AListBox.Items[Index].Enabled;
case AItemTag of
1243501:
begin
ACanvasFont.Color := clBlue;
end;
1280781 :
begin
ACanvasFont.Style := [fsBold];
ACanvasFont.Color := clBlack;
end;
end;
ACanvas.Brush.Color := clWhite;
ACanvas.FillRect(Rect);
AText := AListBox.Items[Index].Text;
ATextRect := Rect;
ATextRect.Left := 20;
ACanvas.DrawTexT(AText, ATextRect, 0);
end;
I have code that creates summary footers at runtime for numeric columns, but I can't get the group summary results to show. I've looked at How to create group summaries at runtime and How to set group summary values and How can create summary footer on runtime? but I'm hitting runtime error:
EcxInvalidDataControllerOperation with message 'RecordIndex out of range'
when the grid is rendering.
This code accepts any TcxGridDBTableView so it would be very easy to put into an existing Delphi form.
procedure SummaryGroup(ASummary: TcxDataSummary; AColumn: TcxGridDBColumn;
AKind: TcxSummaryKind; AFormat: string);
var
sumGroup: TcxDataSummaryGroup;
link: TcxGridTableSummaryGroupItemLink; //TcxDataSummaryGroupItemLink;
item: TcxGridDBTableSummaryItem;
begin
AColumn.Summary.FooterKind := AKind;
AColumn.Summary.FooterFormat := AFormat;
sumGroup := ASummary.SummaryGroups.Add;
link := sumGroup.Links.Add as TcxGridTableSummaryGroupItemLink;
link.Column := AColumn;
item := sumGroup.SummaryItems.Add as TcxGridDBTableSummaryItem;
item.Column := AColumn;
item.Kind := AKind;
item.Position := spGroup;
item.Format := AColumn.Summary.FooterFormat;
end;
procedure AutoAwesum(AView: TcxGridDBTableView);
var
summary: TcxDataSummary;
summing: Boolean;
i: Integer;
dc: TcxGridDBDataController;
col: TcxGridDBColumn;
begin
dc := AView.DataController;
summing := False;
summary := dc.Summary;
summary.BeginUpdate;
try
summary.SummaryGroups.Clear;
dc.BeginFullUpdate;
try
dc.GridView.ClearItems;
dc.CreateAllItems;
for i := 1 to AView.ColumnCount - 1 do
begin
col := AView.Columns[i];
case col.DataBinding.Field.DataType of
ftSmallint, ftInteger, ftWord, ftLargeint, ftAutoInc,
ftLongWord, ftShortint:
begin
summing := true;
SummaryGroup(summary, col, skSum, '#');
end;
ftFloat, ftBCD, ftFMTBcd, ftExtended, ftSingle:
begin
summing := true;
SummaryGroup(summary, col, skSum, '#.##');
end;
ftCurrency:
begin
summing := true;
SummaryGroup(summary, col, skSum, '$#.##');
end;
end;
end;
dc.DataModeController.GridMode := not summing;
AView.OptionsView.Footer := summing;
AView.OptionsView.GroupFooterMultiSummaries := summing;
AView.OptionsView.GroupFooters := gfVisibleWhenExpanded;
finally
dc.EndFullUpdate;
end;
finally
summary.EndUpdate;
end;
end;
What am I missing? Thanks.
Finally had a chance to get back to this. As expected, the changes were few and simple. Here's the code that generically creates group summary headers for each numeric column in a grid. I've left some options commented out in the code that you may want to use.
uses
cxGridDBDataDefinitions;
procedure Summarize(ASummary: TcxDataSummary; AColumn: TcxGridDBColumn;
AKind: TcxSummaryKind; AFormat: string);
var
sumGroup: TcxDataSummaryGroup;
link: TcxGridTableSummaryGroupItemLink;
item: TcxGridDBTableSummaryItem;
begin
AColumn.Summary.FooterKind := AKind;
AColumn.Summary.FooterFormat := AFormat;
AColumn.Summary.GroupKind := AKind;
AColumn.Summary.GroupFormat := AFormat;
AColumn.GroupIndex := -1;
sumGroup := ASummary.SummaryGroups.Add;
link := sumGroup.Links.Add as TcxGridTableSummaryGroupItemLink;
link.Column := AColumn;
item := sumGroup.SummaryItems.Add as TcxGridDBTableSummaryItem;
item.Column := AColumn;
item.Kind := skSum;
item.Position := spGroup;
item.Format := AColumn.Summary.FooterFormat;
end;
procedure AutoAwesum(AView: TcxGridDBTableView);
var
summary: TcxDataSummary;
summing: Boolean;
i: Integer;
dc: TcxGridDBDataController;
col: TcxGridDBColumn;
begin
dc := AView.DataController;
summing := False;
summary := dc.Summary;
summary.BeginUpdate;
try
summary.SummaryGroups.Clear;
dc.BeginFullUpdate;
try
dc.GridView.ClearItems;
dc.CreateAllItems;
for i := 1 to AView.ColumnCount - 1 do
begin
col := AView.Columns[i];
case col.DataBinding.Field.DataType of
ftSmallint, ftInteger, ftWord, ftLargeint, ftAutoInc,
ftLongWord, ftShortint:
begin
summing := true;
Summarize(summary, col, skSum, ',0');
end;
ftFloat, ftBCD, ftFMTBcd, ftExtended, ftSingle:
begin
summing := true;
Summarize(summary, col, skSum, ',.00');
end;
ftCurrency:
begin
summing := true;
Summarize(summary, col, skSum, '$,0.00');
end;
end;
end;
// dc.DataModeController.GridMode := not summing;
// AView.OptionsView.Header := summing;
AView.OptionsView.Footer := summing;
// AView.OptionsView.GroupFooterMultiSummaries := summing;
// AView.OptionsView.GroupFooters := gfVisibleWhenExpanded;
finally
dc.EndFullUpdate;
end;
finally
summary.EndUpdate;
end;
end;
I have put together this code Selected rows into clipboard from a lisview.
procedure TFmainViewTCP.Copy1Click(Sender: TObject);
var
Str:String;
k :Integer;
lItem:TListItem;
begin
repeat
lItem:=lvConnection.Selected;
Str:=lItem.Caption;
for k:=0 to lvConnection.Columns.Count-2 do
begin
Str:=Str+' '+lItem.SubItems[k];
end;
Clipboard.AsText:=Clipboard.AsText+ sLineBreak +Str; {copy into clipboard}
until lItem.Selected=True;
end;
I am not sure if this is working correctly, it does not copy out all the rows for me. Can somebody help me out in this?
Thanks in Advance
your code does not iterate over all of the selected rows. It just works on the first selected. You need to loop on all the items and process the selected ones...
procedure TFmainViewTCP.Copy1Click(Sender: TObject);
var
s, t: String;
i: Integer;
lItem: TListItem;
begin
t := '';
lItem := lvConnection.GetNextItem(nil, sdBelow, [isSelected]);
while lItem <> nil do
begin
s := lItem.Caption;
for i := 0 to lItem.SubItems.Count-1 do
s := s + ' ' + lItem.SubItems[i];
t := t + s + sLineBreak;
lItem := lvConnection.GetNextItem(lItem, sdBelow, [isSelected]);
end;
if t <> '' then Clipboard.AsText := t;
end;
I've a Tlistview with 3 columns, I need from Tcollection object as this follow
FListeDispoProduit := TListeDispoProduit.Create(TProduit);
with (FListeDispoProduit) do
begin
with TProduit(Add) do
begin
Name := 'Produit 01';
CIP := 'A001';
StockQty := 3;
end;
But when I try to put this object into the Tlistview only the first column (Name)is populate
I write this:
for i := 0 to FListeDispoProduit.Count -1 do
Tlistview1.Items.Insert(i).Caption := TProduit(FListeDispoProduit.Items[i]).Name;
I need fill those 3 columns (Name,cip,StockQty ), how can I do this?
Thank you.
hope I be clear.
for i := 0 to FListeDispoProduit.Count -1 do
with ListView1.Items.Add() do begin
Caption := TProduit(FListeDispoProduit.Items[i]).Name;
SubItems.Add(TProduit(FListeDispoProduit.Items[i]).CIP);
SubItems.Add(IntToStr(TProduit(FListeDispoProduit.Items[i]).StockQty));
end;
And add more columns in TListView
Procedure TForm1.FillListView;
var
i: Integer;
ListItem: TListItem;
begin
try
ListView1.Items.BeginUpdate;
try
ListView1.Clear;
for i := 1 to 9 do
with ListView1 do
begin
ListItem := ListView1.Items.Add;
Listitem.Caption := 'Caption '+IntToStr(i);
ListItem.SubItems.Add('Subitem1 '+IntToStr(i));
ListItem.SubItems.Add('Subitem2 '+IntToStr(i));
end;
finally
ListView1.Items.EndUpdate;
end;
except
on E: Exception do
MessageDlg(PWideChar(E.Message), TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0);
end;
end;
How can I get all installed components in TStrings?
I think this code work only within packages:
uses TypInfo, ToolIntf, Exptintf;
procedure GetComponentNames(lst: TStrings);
var
i, k: Integer;
CRef: TClass;
strName: ShortString;
begin
lst.Clear;
for i := 0 to ToolServices.GetModuleCount-1 do
begin
for k := 0 to ToolServices.GetComponentCount(i)-1 do
begin
CRef := TClass(GetClass(ToolServices.GetComponentName(i, k)));
while CRef <> nil do
begin
strName := CRef.ClassName;
if lst.IndexOf(strName) = -1 then
lst.Add(strName);
if str <> 'TComponent' then
CRef := CRef.ClassParent
else
CRef := nil;
end;
end;
end;
end;
Or:
uses ToolsApi;
{....}
var
a, i: Integer;
begin
with (BorlandIDEServices as IOTAPackageServices) do
begin
for a := 0 to GetPackageCount - 1 do
begin
for i := 0 to GetComponentCount(a) - 1 do
begin
{get each component name with GetComponentName(a, i);}
// DoSomething
end;
end;
end;
end;
Thanks for help.
This example doesn't use the OpenAPI, it uses the Registry. It works but it also lists non-visual components amongst other hidden items.
procedure GetComponentNames(lst: TStrings);
var
i, j, iPos: Integer;
Reg: TRegistry;
sComponent: String;
slValues, slData: TStrings;
begin
Reg := TRegistry.Create;
slValues := TStringList.Create;
slData := TStringList.Create;
try
Reg.RootKey := HKEY_CURRENT_USER;
Reg.OpenKey('Software\Borland\Delphi\6.0\Palette', False); // Change reg key where appropriate
Reg.GetValueNames(slValues);
for i := 0 to Pred(slValues.Count) do
begin
lst.Append(slValues[i]);
lst.Append('----------');
slData.Delimiter := ';';
slData.DelimitedText := Reg.ReadString(slValues[i]);
for j := 0 to Pred(slData.Count) do
begin
sComponent := slData[j];
iPos := Pos('.', sComponent);
if (iPos > 0) then
Delete(sComponent, 1, iPos);
lst.Append(sComponent);
end;
end;
finally
slData.Free;
slValues.Free;
Reg.Free;
end; {try..finally}
end;
I'm not saying this is ideal but it does give you a list and a headstart.