I have a grid that is programmatically grouped by a single column. I want the user to be able to add rows to the grid via a button. However, I want the new row to be the last row in the currently focused group. The "insert" key will add a row ABOVE the current record. The down arrow will only add a row to the bottom of the LAST group (as it should) when Appending is enabled.
How can I programmatically (via keystroke or button event) append a new record as the last row in the current group? Calling DataSet.Append doesn't achieve the desired results.
Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.
Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.
Hello John.
Thank you for your message. This is how standard VCL datasets operate- the Insert method inserts new records BEFORE the focused record. ExpressQuantumGrid simply inherits this behavior.
To work around this behavior, you can focus the next record and then insert the new record manually. As a result, the inserted record will be placed below the focused one.
In the attachment, you will find an example that demonstrates this approach when the Navigator's "Insert" button is pressed. We look forward to your feedback once you have had the opportunity to try our solution.
Best regards,
Ingvar.
I'm sorry but the example project that you posted does not implement the desired behavior. The newly inserted row should the the LAST ROW in the current group. Your example places the new row below the current record. After several hours of trial and error, I came up with this solution to implement what should be a baseline feature.
function FocusNextRow(AView : TcxGridDBTableView; StayInGroup : boolean = False) : boolean;
begin
//Expand if on group row and jump to first data record of group
if AView.Controller.FocusedRecord is TcxGridGroupRow then begin
AView.Controller.FocusedRecord.Expand(True);
AView.Controller.FocusedRecordIndex := AView.Controller.FocusedRecordIndex +
AView.GroupedItemCount - AView.Controller.FocusedRecord.Level;
end else begin
//don't append row if on last row
if AView.Controller.FocusedRecordIndex<AView.DataController.RowCount-1 then
//Focus next data record
Result := AView.Controller.GoToNext(True)
else
Result := False;
//Expand if on group row and jump to first data record of group
if AView.Controller.FocusedRecord is TcxGridGroupRow then
if StayInGroup then begin
AView.Controller.GoToPrev(True);
Result := False;
end else begin
AView.Controller.FocusedRecord.Expand(True);
AView.Controller.FocusedRecordIndex := AView.Controller.FocusedRecordIndex +
AView.GroupedItemCount - AView.Controller.FocusedRecord.Level;
end;
end;
end;
procedure AppendRowToGroup(AView : TcxGridDBTableView);
var
AValue : Variant;
begin
with AView.DataController do begin
Assert(AView.GroupedItemCount=1,'This only works if grouped by one column.');
//Save current grouping value
AValue := AView.Controller.FocusedRecord.Values[AView.GroupedColumns[0].Index];
//Move to last row of group;
while FocusNextRow(AView, True) do; //Don't wrap this in BeginUpdate…EndUpdate!!!
BeginUpdate;
//Move to next row in dataset
if FocusedRecordIndex < RecordCount - 1 then begin
FocusedRecordIndex := FocusedRecordIndex + 1;
Insert;
end else
Append;
SetEditValue(AView.GroupedColumns[0].Index, AValue, evsValue);
Post(); //<-- Wish there was a way not to have to commit this record to Dataset yet
EndUpdate;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
AppendRowToGroup(cxGrid1DBTableView1);
cxGrid1.SetFocus;
end;
Hello John.
Thank you for sharing your solution with us, which can be helpful for other customers with a similar task.
As for integrating this feature to our TcxGrid, you can post a separate suggestion to our Support Center. Our developers will consider it and possibly implement in future versions of our controls.
Please let me know if you need any additional clarification.
Best regards,
Ingvar.