Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 5 of 5 total
Thread Bound Grid MultiSelect
Wed, Dec 12 2018 8:14 AMPermanent Link

Eivind

All

I'm probably missing the point here, but how can I get the selected rows in a bound Grid? I have seen some example here regarding using Rows[X][Y] to access the selected rows, but that fails on a bound grid? I get List Index Out of bounds when calling the Rows function. Also, if I select a range of rows I cannot navigate the Data-set as that will reset the selection?

So the question is how to get selected rows in bound grid?

Cheers
Eivind
Wed, Dec 12 2018 8:37 AMPermanent Link

Uli Becker

Eivind,

> I'm probably missing the point here, but how can I get the selected rows in a bound Grid? I have seen some example here regarding using Rows[X][Y] to access the selected rows, but that fails on a bound grid? I get List Index Out of bounds when calling the Rows function. Also, if I select a range of rows I cannot navigate the Data-set as that will reset the selection?

From the Online Help:

TGrid.MultiSelect Property
Elevate Web Builder 2 Manual » Component Reference » TGrid
 
 property MultiSelect: Boolean
Specifies whether multiple rows can be selected in the grid. The SelectedCount property can be examined to find out how many rows are selected and the Selected property can be examined to find out which rows are selected.

Information You can only use the multi-select functionality when the RowSelect property is True. Setting the RowSelect property to False will automatically set the MultiSelect property to False also.

Uli
 

Wed, Dec 12 2018 9:37 AMPermanent Link

Eivind

Uli Becker wrote:

<<<  property MultiSelect: Boolean
Specifies whether multiple rows can be selected in the grid. The SelectedCount property can be examined to find out how many rows are selected and the Selected property can be examined to find out which rows are selected.

Information You can only use the multi-select functionality when the RowSelect property is True. Setting the RowSelect property to False will automatically set the MultiSelect property to False also.>>>

@Uli. Yep, I have seen those. I have set RowSelect to True and I can successfully select multiple rows. My issue lies in iterating them and figure out which row is selected when the grid i bound to a Dataset. As mentioned, the Row[X][Y] property only seams to work on unbound grids. I get an list index out of bound error when trying to access the Row function on the bound grid. Also, If I try to call Dataset.First and then Dataset.Next to run through the rows, then the marked rows are gone. It's probably easy, but I cannot figure out how to do it on a bound grid

Cheers

Eivind
 
Wed, Dec 12 2018 2:30 PMPermanent Link

Uli Becker

Eivind,

> @Uli. Yep, I have seen those. I have set RowSelect to True and I can successfully select multiple rows. My issue lies in iterating them and figure out which row is selected when the grid i bound to a Dataset. As mentioned, the Row[X][Y] property only seams to work on unbound grids. I get an list index out of bound error when trying to access the Row function on the bound grid. Also, If I try to call Dataset.First and then Dataset.Next to run through the rows, then the marked rows are gone. It's probably easy, but I cannot figure out how to do it on a bound grid

You are right: from an earlier post by Tim:
<<
you can't reference the Rows property with a *bound* grid.  The Rows
property is where rows are stored for an un-bound grid.
>>

Maybe there is a more elegant way, but this should work (store the
selected RowIndex in an array of integer and loop through this array to
navigate to the matching record in your dataset):

procedure TForm1.Button1Click(Sender: TObject);
var
   i,z: Integer;
   MySelection: Array of Integer;
begin
   SetLength(MySelection, Grid1.SelectedCount);
   z := 0;

   for i := 0 to Grid1.RowCount -1  do
   begin
      if  Grid1.Selected [i] then
      begin
         MySelection[z] := i;
         inc(z);
       end;
   end;

   for i := 0 to Length(MySelection) -1 do
   begin
      URL.MoveTo(MySelection[i] +1);
MultiLineEdit1.Lines.Add(MyDataset.Columns['MyDatasetColumn'].asString);
   end;
end;

Uli
Wed, Dec 12 2018 2:54 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Eivind,

<< Also, If I try to call Dataset.First and then Dataset.Next to run through the rows, then the marked rows are gone. It's probably easy, but I cannot figure out how to do it on a bound grid >>

Yes, if you navigate in the dataset when there are selected rows, it will reset the selected rows if you do so without holding down the shift or ctrl keys to keep selecting rows.

If you want to get access to which rows are selected, you should use these properties:

https://www.elevatesoft.com/manual?action=viewprop&id=ewb2&comp=TGridControl&prop=SelectedCount
https://www.elevatesoft.com/manual?action=viewprop&id=ewb2&comp=TGridControl&prop=Selected

This is how you would delete all of the selected rows:

procedure TForm1.DeleteButtonClick(Sender: TObject);
var
  I: Integer;
begin
  if Grid1.MultiSelect and (Grid1.SelectedCount > 0) then
     begin
     Grid1.DataSet.DisableControls;
     try
        for I:=0 to Grid1.DataSet.RowCount-1 do
           begin
           if Grid1.Selected[I] then
              begin
              Grid1.DataSet.RowNo:=I;
              Grid1.DataSet.Delete;
              end;
           end;
     finally
        Grid1.DataSet.EnableControls;
     end;
     end
  else
     Grid1.DeleteRow;
end;

I'm probably going to make the TGrid's DeleteRow method use the multi-selected for deletion in a future release, in order to avoid all of this code.

Tim Young
Elevate Software
www.elevatesoft.com
Image