Jump to content


Photo

Problem with Programmatically Selecting a Row.


  • Please log in to reply
14 replies to this topic

#1 Speed

Speed
  • Members
  • 190 posts
  • Gender:Male

Posted 15 March 2016 - 01:58 AM

Hi Boki,

 

I am using a TNextGrid (V5) component with the [goHeader,goMultiSelect,goSelectFullRow] options set.

 

When I set a row programmatically (MyGrid.Selected[i] := True), the row looks selected and if I check the SelectedCount it returns a value of 1, but when I try to access the row using SelectedRow it returns -1 and generates a "List Index Out of Bounds (-1)" error.


If the row is then selected again by clicking on it, it works fine (SelectedRow returns the no of the row).

 

Is this a bug or am I doing something wrong?

 

Thanks,

 

Steve



#2 Speed

Speed
  • Members
  • 190 posts
  • Gender:Male

Posted 15 March 2016 - 02:25 AM

Hi Boki,

 

Following up to my previous post, I have added some code to check to see which row is selected:

 

      if grdMessages.SelectedRow < 0 then
      begin
         for i := 0 to grdMessages.RowCount - 1 do
         begin
            if grdMessages.Selected[i] then
            begin
               CodeSite.SendMsg('Row ' + IntToStr(i) + ' is selected.');
            end;
         end;
      end;

 

CodeSite showed that row 9 is selected, but SelectedRow thinks there are no rows selected because it is returning -1.

 

Is this something to do with the grid being configured to allow multiple rows?  As mentioned in the last email, if I click on the row using the mouse then the code works fine, it's only when I set it in code that it doesn't work.

 

Regards,

 

Steve



#3 Speed

Speed
  • Members
  • 190 posts
  • Gender:Male

Posted 18 March 2016 - 01:29 AM

Hi Boki,

 

Have you had a chance to look at this yet?

 

Regards,

 

Steve



#4 Speed

Speed
  • Members
  • 190 posts
  • Gender:Male

Posted 31 March 2016 - 01:01 AM

Hello?



#5 deonvn

deonvn

    Senior Member

  • Honorable Members
  • PipPip
  • 313 posts

Posted 31 March 2016 - 09:40 AM

Hi Steve

 

This has always been the case. SelectedCount returns the number of rows that are currently selected in a multi-select situation. Even if SelectedCount is greater than zero, SelectedRow might still be -1.

 

Because of this, you should always check if the row indicated by SelectedRow actually exists before using it.

if NextGrid1.RowExist(NextGrid1.SelectedRow) then
  begin
    ...
  end;

After you've indicated which rows are selected (in multi-select environment) you should then also indicate which one of the selected rows is the "currently selected" row (there can be only one).

for I := 0 to NextGrid1.RowCount -1 do
  begin
    if I mod 2 = 0 then
      NextGrid1.Row[I].Selected := True;
  end;

NextGrid1.SelectedRow := 4;

 



#6 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 31 March 2016 - 10:50 AM

Hi,

Sorry for missing this thread,

Since selected row can be un-clicked, there is a possibility to have SelectedRow as -1.

Deonvn post have all info about this.
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#7 Speed

Speed
  • Members
  • 190 posts
  • Gender:Male

Posted 01 April 2016 - 12:56 AM

Hi Deonvn and Boki,

 

Thanks.  This seems like odd behaviour to me.  Is this documented anywhere?

 

So even though I select a row in code, and the row shows as being selected, SelectedRow can still be equal to -1 one even though the user hasn't un-clicked it??

 

= Steve



#8 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 01 April 2016 - 01:56 AM

Hi,

In multi select mode is all about Selected[] property. It was a long ago, but problably I wanted to split this modes.
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#9 Speed

Speed
  • Members
  • 190 posts
  • Gender:Male

Posted 01 April 2016 - 06:13 AM

Sometimes a row visually appears to be selected but is in fact not selected (let's call it row 1 for this example).  If another row is then clicked on/selected without using <ctrl> or <shift> (let's call it row 2) then it looks like two rows are selected (row 1 and row 2) when actually only the row just clicked/selected is selected (row 2).

 

What is the best way to change row 1 so that it doesn't look selected while still keeping row 2 selected?

 

Thanks,

 

Steve



#10 Speed

Speed
  • Members
  • 190 posts
  • Gender:Male

Posted 01 April 2016 - 07:09 AM

Actually this appears to be a slightly different issue.  It's allowing me to sometimes select multiple rows without using the <ctrl> or <shift> keys.



#11 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 01 April 2016 - 11:57 AM

Hi,

Maybe it's shows dotted line (focus)?

If is blue (highlighted) then is selected (Selected[row] = True). Can you plaese upload one screenshot, or send me a small demo project?
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#12 Speed

Speed
  • Members
  • 190 posts
  • Gender:Male

Posted 15 April 2016 - 05:15 PM

Hi Boki,

 

Sorry for the delay in responding.  I have been caught up with some big releases.  I have sent you a demo project as requested.

 

Thanks,

 

Steve



#13 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 16 April 2016 - 12:57 PM

Thanks Steve,

 

I will look at it soon.


boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#14 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,191 posts
  • Gender:Male

Posted 16 April 2016 - 08:52 PM

Hi Steve,

 

Thank you again for demo,

 

I think that problem is actually because gtMultiSelect is not set, but Selected[x] is called.

 

Then, after new row is selected Deselect method is not called (to deselect already selected rows). I have done this due to optimisation (to not call always Deselect method in non-multiselect mode).

 

So, I suggest always using gtMultiSelect when you set Selected[x] := True;

 

If you need to indicate rows in different way, but not have them selected maybe you can use OnCellColoring event?


boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#15 Speed

Speed
  • Members
  • 190 posts
  • Gender:Male

Posted 17 April 2016 - 03:21 AM

Hi Steve,

 

Thank you again for demo,

 

I think that problem is actually because gtMultiSelect is not set, but Selected[x] is called.

 

Then, after new row is selected Deselect method is not called (to deselect already selected rows). I have done this due to optimisation (to not call always Deselect method in non-multiselect mode).

 

So, I suggest always using gtMultiSelect when you set Selected[x] := True;

 

If you need to indicate rows in different way, but not have them selected maybe you can use OnCellColoring event?

 

 

 

Hi Boki,

 

It happens with Multiselect as well.  I have amended the demo project to show this and sent it to you.

 

Regards,

 

Steve






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users