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?
 
techsuppoprtConnect With a Mentor Author 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
 
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
techsuppoprtAuthor Commented:
...added points
0
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.