[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to jump to next selection in a datagridview.....or jump between seleted rows

Posted on 2008-11-14
17
Medium Priority
?
162 Views
Last Modified: 2013-12-17
Hi

I have a datagridview with checkbox selection rows,every time i select cntrl-J i want to jump to next selection checkd in my datagridview.....
private void ReconManuallyGridView_KeyDown(object sender, KeyEventArgs e)
        {
 
            if (e.KeyData == (Keys.Control | Keys.Z))
            {
                UndoLastReconciled(this.myOper);
            }
            if (e.KeyData == (Keys.Control | Keys.J))
            {
                JumpToNextReconcildRow();
            }
        }
        private bool nextCell = false;
 
        private void JumpToNextReconcildRow()
        {
            if (!m_isChangingCell)
            {
                nextCell = true;
 
                this.ReconManuallyGridView.CurrentCell = ReconManuallyGridView[3, ReconManuallyGridView.CurrentRow.Index];
 
                nextCell = false;
            }
        }

Open in new window

0
Comment
Question by:Adnan
  • 11
  • 6
17 Comments
 
LVL 8

Expert Comment

by:jorgesv13
ID: 22959683
First, you have to get the current index, and then loop in the next rows to see which one is checked

    int curr = ReconManuallyGridView.CurrentRow.Index;
    for (int i = curr; i <= ReconManuallyGridView.Rows.Count - 1; i++) {
        
        //change 0 for the index of your checkbox column
        if (ReconManuallyGridView[0, i].Value == true) {
            ReconManuallyGridView.CurrentCell = ReconManuallyGridView[3, i];
        }
    }
    

Open in new window

0
 

Author Comment

by:Adnan
ID: 22959842
Hi jorgesv13:


I get error on this  line if (ReconManuallyGridView[0, i].Value != false)
error: Error      6      Operator '!=' cannot be applied to operands of type 'object' and 'bool'      

               
0
 
LVL 8

Expert Comment

by:jorgesv13
ID: 22959911
you should do a casting to bool before making the comparison

if ((bool)ReconManuallyGridView[0, i].Value != false)
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:Adnan
ID: 22959941
hmm oki, but know i get exeption saying that specified cast is not valid...?
0
 
LVL 8

Expert Comment

by:jorgesv13
ID: 22959963
Are you sure yout first column is your checkbox column?
You should replace the "0" for the correct index.
Or maybe you can try specifying the name of the column.
Example:
if ((bool)ReconManuallyGridView["myCheckboxColumn", i].Value != false)
0
 

Author Comment

by:Adnan
ID: 22960037
hmm oki, its working know, but if i hvae more then one row checked i want to loop in selection and stop when it comes to last selection....?
0
 

Author Comment

by:Adnan
ID: 22960048
i mean dont stop on last selection..!! loop throow
0
 
LVL 8

Expert Comment

by:jorgesv13
ID: 22960063
add a break instruction just after the line where you set the CurrentCell

 ReconManuallyGridView.CurrentCell = ReconManuallyGridView[3, i];
break;
0
 

Author Comment

by:Adnan
ID: 22960132
i set the break point as you sayd but know it has stop jumping......it stays on the same selection and not jumping throow....
0
 
LVL 8

Expert Comment

by:jorgesv13
ID: 22960187
ah.. maybe it's staying in the first
try adding one to the initial iterator:

 for (int i = curr +1; .....
0
 

Author Comment

by:Adnan
ID: 22960197
without break point it jumps to last index and the 2 or 3 index....?
0
 

Author Comment

by:Adnan
ID: 22960200
and not
0
 

Author Comment

by:Adnan
ID: 22960222
oki but how can i correct this?
0
 
LVL 8

Accepted Solution

by:
jorgesv13 earned 2000 total points
ID: 22960305
Did you try adding one to the initial iterator?

Replacing:

for (int i = curr; i <= ReconManuallyGridView.Rows.Count - 1; i++) {

for

for (int i = curr+1; i <= ReconManuallyGridView.Rows.Count - 1; i++) {

Please also post your complete code
0
 

Author Comment

by:Adnan
ID: 22973755
Hi  jorgesv13:

*yes i try adding one to intitial iterator it worked fine, but if selection is on last selection it wont jump to first one, how can i jump from last selection to first?


private void JumpToNextReconcildRow()
        {

            int curr = ReconManuallyGridView.CurrentRow.Index;
            //for (int i = curr; i <= ReconManuallyGridView.Rows.Count - 1; i++)
            for (int i = curr + 1; i <= ReconManuallyGridView.Rows.Count - 1; i++)
            {
                //change 0 for the index of your checkbox column
                if ((bool)ReconManuallyGridView["recon", i].Value != false)
                {
                    ReconManuallyGridView.CurrentCell = ReconManuallyGridView["recon", i];
                   
                    break;
                }


            }
        }
0
 

Author Comment

by:Adnan
ID: 22974177
I MADE IT.....      ;)

here is the code.....

 private void JumpToNextReconcildRow()
        {
            int counter;
            int curr = ReconManuallyGridView.CurrentRow.Index;
            for (int i = curr + 1; i <= ReconManuallyGridView.Rows.Count - 1; i++)
            {
               
                if ((bool)ReconManuallyGridView["Recon", i].Value == true)
                {
                    ReconManuallyGridView.CurrentCell = ReconManuallyGridView["Recon", i];
                    return;
                }
           }
           for (int i = 0; i <= curr; i++)
           {
               
               if ((bool)ReconManuallyGridView["Recon", i].Value == true)
               {
                   ReconManuallyGridView.CurrentCell = ReconManuallyGridView["Recon", i];

                   return;
               }

           }

        }
0
 

Author Closing Comment

by:Adnan
ID: 31516764
thanks
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question