[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DataGrid right click row selection quesion

Posted on 2008-11-11
16
Medium Priority
?
2,211 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:techsuppoprt
  • 8
  • 6
14 Comments
 
LVL 7

Expert Comment

by:aherps
ID: 22937698
Hi - have a look at this website. It discusses the issues and a solution:

http://www.knowdotnet.com/articles/datagridrightmouseselect.html
0
 
LVL 1

Author Comment

by:techsuppoprt
ID: 22941393
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
 
LVL 1

Author Comment

by:techsuppoprt
ID: 22943070
...added points
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:aherps
ID: 22943651
In the cellMouseClick event, add:



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

Open in new window

0
 
LVL 7

Expert Comment

by:aherps
ID: 22943675
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
 
LVL 1

Author Comment

by:techsuppoprt
ID: 22945120
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
 
LVL 1

Accepted Solution

by:
techsuppoprt earned 0 total points
ID: 22945213
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
 
LVL 7

Expert Comment

by:aherps
ID: 22945786
RowHeaderVisible = False
0
 
LVL 7

Expert Comment

by:aherps
ID: 22945795
And in code:

m_grid.RowHeader.Visible = false;

Open in new window

0
 
LVL 1

Author Comment

by:techsuppoprt
ID: 22945800
yeah.. got it, thanks. Was looking for that property name. I'll try it out.
0
 
LVL 1

Author Comment

by:techsuppoprt
ID: 22945859
actually the first time you were correct :)

m_grid.RowHeadersVisible = false;
0
 
LVL 7

Expert Comment

by:aherps
ID: 22945879
:)

I got carried away with the .Visible!!

Glad you sorted it out!
0
 
LVL 1

Author Comment

by:techsuppoprt
ID: 22982145
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
 
LVL 1

Author Comment

by:techsuppoprt
ID: 23579270
Bob, see my post above re distributing points.(ID: 22982145)
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

872 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