Datagridview Determine if right click is on selected cells

VB 2005

How do I tell if a right click on a datagridview is in the selected
cells? I want to selected the cell if it's not, and if it is in the
selected cell range do nothing.

I have this code to select the right clicked cell, but how do I check
if it's in the selected cell range?

Private Sub DataGridView_CellMouseDown(ByVal sender As System.Object,
_
 ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) _
 Handles DataGridView.CellMouseDown

	If e.Button = Windows.Forms.MouseButtons.Right Then
		Me.DataGridView.CurrentCell =
Me.DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex)
	End If

End Sub
0
Bill
2/11/2010 5:19:45 PM
dotnet.languages.vb 830 articles. 0 followers. Follow

3 Replies
2050 Views

Similar Articles

[PageSpeed] 37

you can try something like this:

Dim SelectedCells As DataGridViewSelectedCellCollection =
datagridview1.SelectedCells

For Each cel As DatagridviewCell in SelectedCells
   Debug.Print cel.ColumnIndex.ToString & " " cel.RowIndex.ToString
Next 

Rich

*** Sent via Developersdex http://www.developersdex.com ***
0
Rich
2/11/2010 7:01:00 PM
On Feb 11, 1:01=A0pm, Rich P <rpng...@aol.com> wrote:
> you can try something like this:
>
> Dim SelectedCells As DataGridViewSelectedCellCollection =3D
> datagridview1.SelectedCells
>
> For Each cel As DatagridviewCell in SelectedCells
> =A0 =A0Debug.Print cel.ColumnIndex.ToString & " " cel.RowIndex.ToString
> Next
>
> Rich
>
> *** Sent via Developersdexhttp://www.developersdex.com***

Thanks, got this work with reasonable performance:

If e.Button =3D Windows.Forms.MouseButtons.Right Then
	Dim SelectedCells As DataGridViewSelectedCellCollection =3D
Me.DataGridView.SelectedCells
	Dim bInselection As Boolean =3D False

	'Check to see if click in selected range.. if it is do nothing
	For Each cel As DataGridViewCell In SelectedCells
		If e.ColumnIndex >=3D cel.ColumnIndex AndAlso _
		 e.ColumnIndex <=3D cel.ColumnIndex AndAlso _
		 e.RowIndex >=3D cel.RowIndex AndAlso _
		 e.RowIndex <=3D cel.RowIndex Then

			bInselection =3D True
			Exit For
		End If
	Next

	'Select the cell, if it isn't in the current selected range
	If Not bInselection Then _
	   Me.DataGridView.CurrentCell =3D
Me.DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex)

End If
0
Bill
2/11/2010 9:29:36 PM
On Feb 11, 3:29=A0pm, Bill Schanks <wscha...@gmail.com> wrote:
> On Feb 11, 1:01=A0pm, Rich P <rpng...@aol.com> wrote:
>
> > you can try something like this:
>
> > Dim SelectedCells As DataGridViewSelectedCellCollection =3D
> > datagridview1.SelectedCells
>
> > For Each cel As DatagridviewCell in SelectedCells
> > =A0 =A0Debug.Print cel.ColumnIndex.ToString & " " cel.RowIndex.ToString
> > Next
>
> > Rich
>
> > *** Sent via Developersdexhttp://www.developersdex.com***
>
> Thanks, got this work with reasonable performance:
>
> If e.Button =3D Windows.Forms.MouseButtons.Right Then
> =A0 =A0 =A0 =A0 Dim SelectedCells As DataGridViewSelectedCellCollection =
=3D
> Me.DataGridView.SelectedCells
> =A0 =A0 =A0 =A0 Dim bInselection As Boolean =3D False
>
> =A0 =A0 =A0 =A0 'Check to see if click in selected range.. if it is do no=
thing
> =A0 =A0 =A0 =A0 For Each cel As DataGridViewCell In SelectedCells
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 If e.ColumnIndex >=3D cel.ColumnIndex And=
Also _
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0e.ColumnIndex <=3D cel.ColumnIndex And=
Also _
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0e.RowIndex >=3D cel.RowIndex AndAlso _
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0e.RowIndex <=3D cel.RowIndex Then
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 bInselection =3D True
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Exit For
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 End If
> =A0 =A0 =A0 =A0 Next
>
> =A0 =A0 =A0 =A0 'Select the cell, if it isn't in the current selected ran=
ge
> =A0 =A0 =A0 =A0 If Not bInselection Then _
> =A0 =A0 =A0 =A0 =A0 =A0Me.DataGridView.CurrentCell =3D
> Me.DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex)
>
> End If

For those interested I found a much cleaner approach:

Private Sub DataGridView_CellMouseDown(ByVal sender As System.Object,
_
 ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) _
 Handles DataGridView.CellMouseDown

	If e.Button =3D Windows.Forms.MouseButtons.Right AndAlso _
	   Not
Me.DataGridView.SelectedCells.Contains(Me.DataGridView.Rows(e.RowIndex).Cel=
ls(e.ColumnIndex))
Then _
		Me.DataGridView.CurrentCell =3D
Me.DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex)

End Sub
0
Bill
2/11/2010 9:51:37 PM
Reply:

Similar Artilces:

Conditional formatting depending on sub string content i cell
Hi! This has been eluded me all afternoon, I just want to mark out cells in Excel that contains a specific sub string. I have thousands of rows all containing small text strings. I would like to mark the once containing one specific word with blue text, and other cells containing another specific key word with a different color. Very simple, don't care about corner cases (containing several key words etc.) Mostly I'm annoyed that I cannot solve this simple problem. I just cannot get Excel to understand any of the formulas I try in conditional formatting. Can anyone h...

HELP! No One has system admin rights
Can anyone help. One of our CRM users has managed to remove the system admin role from the domain administrator account!! We're only using the MSDN licences and none of the other users have system admin roles, their all either marketing staff of sales staff. Any help would be great thanks in advance. well john if ur domain administrator is the account with which u installed CRM then what u can do is log on crm machine with system admin and go to deployment manager and create another temporary user with the system administrator role and then use it to provide the system adminitrator ri...

worksheet cell references
The below is an example of a formula that I was shown to get values from other worksheets into my current worksheet, using a cell to reference the appropriate worksheet needed. Is there a way to write this so that the additional string cell reference is not absolute. I would like to copy and paste, but the only reference that changes is the A1. I know that if I put a $ in front of the 1, the A$1 will always reference the same cell, when copied and pasted, for the worksheet name I need to use, but I need to get different information from sequential cells in the worksheets. =INDIRECT...

The terror of advancing cell numbers!
Hi everybody, I apologize for this really obvious problem, I haven't encountered i before so its really ummm "bugging" me. I am using the vlookup function, and in the formulae it refers to range of figures A1-Z100 in another worksheet. If you copy and past the formulae to find the next piece of information xcel automaticall advances the the references, so that now the range of figures it refer to is A2-Z101, which means that the vlookup function is no longe looking in the full range of figures. How do I stop it from doin this? In the past I just did one row, manually changing...

Preventing an application exit from the task bar's right click Close menu
Hello gentlemen, I am developping an application that uses the system tray (I want it to behave like MSN Messenger) That being said, I am almost there. There is one issue I could not solve and it is how to distinguish and prevent my application to quit when the user right-clicks on the application's entry on the task bar and selects close when the UI is visible. I want the application to only minimize when this action is performed. I was able to prevent this in a way but it is not the proper way I beleive because when I try to kill the application from the Task Manager, the &quo...

Excel embedded in Word and Select range to copy
Hello, I have been looking into how I can select an embedded excel doc in Word 2007 and copy all the details in the worksheet to a bookmark in the active document. I have managed to open the embedded workbook but I am now stuck. Code so far is : Sub ActivateExcelCell() Dim ils As Word.InlineShape Dim doc As Word.Document Dim of As Word.OLEFormat Dim xlWB As Excel.Workbook Dim xlapp As Excel.Application Set of = ActiveDocument.Range.InlineShapes(1).OLEFormat of.DoVerb wdOLEVerbPrimary Set xlWB = of.Object ...

Number & Text in same cell
Hello Excel Wizards! Is there a way to display a number and some text (like 2 or 3 letters) in the same cell, using *"format cells, custom"* function. Example: Display 28 tons in a cell as 28 tns. The 28 will keep updating to a different value every so many minutes, but the tns, need to remain in the cell to reflect the updated value in tons. Sometimes I need to use t/h for tons per hour. Not sure if this is possible. I have, in the past, placed the unit in a cell next to the number, but in many other cases this is just not good for me. Thanks, EMoe -- EMoe ------------------...

Decompile right click
Hello All pretty simple question for you all. just upgraded to office 2007, but now my access files dont have the right click compact or decompile. how do i set this up again? -- As always, any and all help appreciated! :) That was never a part of the program. It was an something added. Have a look here: http://www.mvps.org/access/modules/mdl0039.htm -- Arvin Meyer, MCP, MVP http://www.datastrat.com http://www.accessmvp.com http://www.mvps.org/access "DawnTreader" <DawnTreader@discussions.microsoft.com> wrote in message news:60EDFCAD-1FE0-4EE9-A7C1...

my messages do not appear in my inbox without clicking on send/re.
What version of Outlook are you using? Also, how often do you have Outlook polling your mailbox? "misterdoo" <misterdoo@discussions.microsoft.com> wrote in message news:8E777A96-B06E-4922-A8D3-5D7E563D039F@microsoft.com... > ...

Different text selection
Probably a stupid but... I created a newsletter from a template. When I select some text (e.g. the headlines & sample "stories") the whole item is selected as a block, which can be dragged to a new area, but not altered (except by being overwritten). How is this created? as opposed to ordinary text, which is selectable character by character. Chris how about you tell us what version of Windows and Publisher you are using, and which Template? We are good, and some of us very good, and a few are excellent, but hell, our crystal balls cannot cope with the workload. -- B...

Determine keys pressed while clicking
Hello, In my application I need to figure out which modifier keys (ALT, CTRL and SHIFT) are held when the user clicks somewhere. I can easily figure out about the Shift and Ctrl key, since they're sent in the nFlags parameter of OnLButtonDown through MK_SHIFT and MK_CONTROL. But apparently MK_ALT isn't part of that, for some reason. Previously, I tried to grab the keystate with the GetKeyState function, except during beta testing it turned out it doesn't always work for everyone (I never had any problems myself, but some others did). Is there another way to find out which ...

Cannot format cells
I have a spreadsheet that will not allow me to format most of the cells. If I right click on any cell or range of cells and pick Format Cells nothing happens. The dialog box does not open. I can format some of the cells on the sheet but there is no rhyme or reason to it. There are some borders of differing types and this seems to be the problem as I can copy the spreadsheet to a new page, delete all the borders and then re-do them identically to the original and then the Format Cells dialog box works. I can copy a format from one cell to another and that works too. There are no m...

From VBA: How to select a column by column number instead of letter?
I'm in MS Access and don't know from column letters - all my constants are in terms of column number. I'd like to select, say, column number 7 and hide it.... ? -- PeteCresswell in a macro columns(7).hidden=true -- Don Guillett SalesAid Software donaldb@281.com "(Pete Cresswell)" <x@y.z> wrote in message news:i117011tjd8g1bimpn0dmk2oc1e901bbvn@4ax.com... > I'm in MS Access and don't know from column letters - all my constants are in > terms of column number. > > I'd like to select, say, column number 7 and hide it.... > > ? > -...

Why do I only see this lookup result when I click on it?
I have a continous form control that only displays the results when I click on it (them). Can someone help me make it so they display permantly. the name of the control is: txt_Timer_Elapsed the code for the control is: =DLookUp("Timer_Elapsed","QRY_Run_Reveal_Timings","Run_waypoint_List_ID = " & [Run_waypoint_List_ID]) Add the necessary table and include the field Timer_Elapsed in the recordsource for the form, then make a bound control to show the value for Timer_Elapsed. Jeanette Cunningham "efandango" <efandango@discussions.micro...

Access read the selected value of a Combobox in Excel
I have a standard document that is filled out by users in Excel, and am having difficulty getting Access to read the selected value of a combobox in the Excel sheet, and populate a textbox on an Access form. Any ideas on how to code this, I'm all out. Thanks! ...

KB309801 is not quite right
Hi, The "resolution" section is too terse/inflexible. This way it'll become more helpful: #if defined(_AFXDLL) && (_MFC_VER >= 0x0700) #include <atlstr.h> template class __declspec(dllimport) CStringT<TCHAR, StrTraitMFC<TCHAR, ChTraitsCRT<TCHAR> > >; template class __declspec(dllimport) CSimpleStringT<TCHAR>; #endif Please notice: _AFXDLL, #include Regards, Troels ...

Protecting Selected Cells and Functions
I have a worksheet. In Cell B2 is a Data validation box Listing a range of colleagues names( DRop Down Menu). On selection of a name in B2, the contents of the whole worksheet changes. I like to Protect the worksheet for: 1) Hiding the formulaes 2) And most importantly preventing editing of the contents of any other cell (except B2). and yet be permiitted to: 3) Select contents in Cell B2 (Data Validation Box) 4) Select Auto filters in Row 4 I've tried using the the Tools/ Protect worksheet menu, ticking Select Lock Cells, Select Unlock cell, use auto filters. And in in Format/Cells/...

how to select customers from a table
I have a table that multiple users will use at the same time. It basically consists of a list of customers names. I want to be able to select multiple customers from that table when I am creating a new "project" so when I print out the project report, the "customers" list on the report is the customers that were selected from that table. How can I do that without locking the table from other users? -- Message posted via http://www.accessmonster.com On Fri, 10 Aug 2007 23:22:45 GMT, "kallen via AccessMonster.com" <u6724@uwe> wrote: >I have a table ...

DataGridView Events
What are the events handlers I need to insert / update / delete rows in a datagridview? Seems a lot different than asp.net. I can initially bind the datagridview but don't know the event handlers to add/update/delete. I'd appreciate any insight. Thanks a lot. If you create your own made data source, one way is to implement IBindingList and to define an empty constructor (to add new "row"). BindingList><T> does implement IBindingList. You can use BindingSource to expose IBindingList on types that do not implement IBindingList. Here an example...

can I import excel cells into word as labels
I am using Office 2000, and I'm trying to import excel data into word as labels. I have one column of numbers(approx. 350 cells) I would like to import into Word as individual labels. can this be done? -- Trial and Error takes too long! Use Mail merge. :) http://www.officearticles.com/word/mail_merge_labels_in_microsoft_word.htm ************ Anne Troy www.OfficeArticles.com "wichita6" <wichita6@discussions.microsoft.com> wrote in message news:E79D3E90-8093-4B9F-B195-DF6C4C54ED0D@microsoft.com... >I am using Office 2000, and I'm trying to import excel data into ...

CTreeCtrl and full row selection
Hello. I'm using CTreeCtrl in my MDI application (something like the CTReeCtrl of Windows Explorer). Currently, a user may select an item only if he hovers over the item text and presses the mouse L Button. I want that an item in the tree will be selected when the user presses the mouse L Button anywhere inside the row, and not only on the text. I tried adding a FullRowSelection style to the tree, but in that case all the row is highlighted and it looks very bad, I want that only the item text will be highlighted. In Windows Explorer it works fine somehow: When you press on any location in...

Put a checkbox in the header of the datagridview
Hi, How can I put a checkbox in the header of the datagridview? Thanks ...

How do I make a cell required to edit in Excell?
I created a template Accounting Sheet for my office to use but they constantly forget to fill in the date. How can I make that field required to edit before saving? You could use conditional formatting to fill the other fields with black, unless the date cell has a value in it. For example, if the date goes in cell D3: Select the other cells where data should be entered Choose Format>Conditional Formatting From the first dropdown, choose Formula Is In the Formula box, type: =$D$3="" Click the Format button On the Pattern tab, select the black colour Click OK, click OK Be...

Text appear in formula bar but not in cell
Hi, With reference to the subject, I have tried Wrap text and Rows/Columns Autofit but nothing works. Also all the typed text does not appear in the printout. Please advise. Try this. Go to Edit -- Clear -- Formats. I think this will solve the problem. If not mail me the sheet on sanskar_d@rediffmail.com -- sanskar_d ------------------------------------------------------------------------ sanskar_d's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=24217 View this thread: http://www.excelforum.com/showthread.php?threadid=378472 Usman, Is your font colour...

Before Print for All Selected sheets.
Good afternoon all. Getting quite frustrated trying to code this myself and I just can't figure out the correct syntax for what I want to do. I have a Workbook BeforePrint() sub that obviously, gets called whe the user goes to printpreview, or prints the workbook (I have a prin dialogue box that does this for me). What I want to do, is add the following code to each selecte worksheet, or all worksheets if none selected. ActiveSheet.PageSetup.LeftHeader = "Printed By " Application.UserName How would I get Excel to add this header ("Printed By " Application.UserName...