DataGrid right click row selection quesion

Hi Experts.
Need a bit of a help here please.

I have a DataGridView processing class with various methods ( shown below ).
One of the methods enables Right Mouse click row selection in the DataGridView.

Something is wrong with it though... Right click seems to highlight the row but it doesn't actually select it. The SelectedIndex remains on the first row unless I left click elsewhere.
I can also see a little triangle in the first gray column, indicating selected row, doesn't switch over to right clicked row.

Anyone can tell me whats going on here ?

By the way.. is it possible to disable that first column in the DataGridView that indicates the selected row ?

Thank you in advance.
class GridViewProcessing
    {
        DataGridView m_grid;
 
        public GridViewProcessing(DataGridView _grid)
        {
            m_grid = _grid;
        }
 
        //This will enable Right click row selection
        public DataGridView EnableRightClickSelection(int x, int y)
        {
            //Enable DataGridView right click selection
            DataGridView.HitTestInfo ht;
            ht = m_grid.HitTest(x, y);
 
            if (ht.Type == DataGridViewHitTestType.Cell)
            {
                if (!((DataGridViewRow)(m_grid.Rows[ht.RowIndex])).Selected)
                {
                    m_grid.ClearSelection();
                    ((DataGridViewRow)m_grid.Rows[ht.RowIndex]).Selected = true;
 
                }
            }
            return m_grid;
        }
 
            
 
    }

Open in new window

LVL 1
techsuppoprtAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aherpsCommented:
Hi - have a look at this website. It discusses the issues and a solution:

http://www.knowdotnet.com/articles/datagridrightmouseselect.html
0
techsuppoprtAuthor Commented:
Thanks.

That code is for the DataGrdi, not DataGridView though.
While very close the code is not the same .

I also just tried this ( below ) and it still doesn't select..the right click  just highlights the row but doesnt actually select it.
 Any idea guys ?
        public GridViewProcessing(DataGridView _grid)
        {
            m_grid = _grid;
        }
 
        //This will enable Right click row selection
        public DataGridView EnableRightClickSelection(int _x, int _y)
        {
            //Enable DataGridView right click selection
            DataGridView.HitTestInfo ht = m_grid.HitTest(_x,_y);
 
            if (ht.Type == DataGridViewHitTestType.Cell)
            {
                if (!((DataGridViewRow)(m_grid.Rows[ht.RowIndex])).Selected)
                {
                    m_grid.ClearSelection();
                    m_grid.Rows[ht.RowIndex].Selected = true;
                }
            }
            return m_grid;
        }

Open in new window

0
techsuppoprtAuthor Commented:
...added points
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

aherpsCommented:
In the cellMouseClick event, add:



If (e.Button = Windows.Forms.MouseButtons.Right)
DataGridView1.Rows(e.RowIndex).Selected = True;

Open in new window

0
aherpsCommented:
Oooh - and to avoid error when clicking header: (And a c# adjustment from VB)



If ((e.Button == Windows.Forms.MouseButtons.Right) && (e.RowIndex >= 0))
DataGridView1.Rows[e.RowIndex].Selected = True;

Open in new window

0
techsuppoprtAuthor Commented:
Same thing bud.. It highlights the row in blue but it doesn't actually select it.

The selected index remains 0 and the triangle in the first column indicates that the first row is selected still.
0
techsuppoprtAuthor Commented:
Found an answer.
This was a tip:

http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=4094059&SiteID=1&pageid=0

The cell needs to be selected for the selection index to be set on the row.
Adding the line below fixed it.

By the way.. does anyone know the answer to the second part of the quesion ?
                    m_grid.CurrentCell = m_grid.Rows[ht.RowIndex].Cells[m_grid.Columns.Count-1];

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aherpsCommented:
RowHeaderVisible = False
0
aherpsCommented:
And in code:

m_grid.RowHeader.Visible = false;

Open in new window

0
techsuppoprtAuthor Commented:
yeah.. got it, thanks. Was looking for that property name. I'll try it out.
0
techsuppoprtAuthor Commented:
actually the first time you were correct :)

m_grid.RowHeadersVisible = false;
0
aherpsCommented:
:)

I got carried away with the .Visible!!

Glad you sorted it out!
0
techsuppoprtAuthor Commented:
I'm not quite sure how to distribute points here.

I want to mark my answer as "The Answer" but award points to 'aherps' for answering the second part of the question. Hopefully the cleanup crew will take care of it.

Thank you.
0
techsuppoprtAuthor Commented:
Bob, see my post above re distributing points.(ID: 22982145)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.