Preventing selection of cell in datagridview

drmike

Member
Joined
Mar 3, 2006
Messages
13
Programming Experience
10+
How can I stop a user from selecting/entering a cell or column in a datagridview please?
Mike
 
As far as I'm aware there is no easy way to prevent a user selecting a cell, but nor is there any reason to do so. Both the DataGridViewColumn and DataGridViewCell classes have a ReadOnly property. You can set that to True to make a column or cell uneditable, even if the rest of the grid can be edited.
 
Thanks for the reply. I'm aware of the readonly setting but I wanted to use the grid as a data entry form layout with columns 1 & 3 holding captions for data entries made in columns 2 & 4. So, in that case I wouldn't want the user to be able to click into columns 1 & 3 if you see what I mean.

I'm a bit disappointed I can't do this but I can cope. I'll just use a bunch of text boxes but a datagridview would have suited me better.

Mike
 
If you are aware of the ReadOnly property, why not use it?
I believe you're only aware of the ReadOnly property of the DataGridView. As jmcilhinney stated, the DataGridViewColumn also has a ReadOnly property. This means that you can make columns 1 & 3 ReadOnly while at the same time allowing data entry in columns 2 & 4.
To do this, select the DataGridView, in the property explorer click the ellipses button (the button with '...'), in the dialog select the DataGridViewColumn on the left and on the right set the ReadOnly property for the individual columns.
 
Sorry I thought I made it clear.

What I want to prevent is any cell in columns 1 & 3 getting focus, so if the user clicks into any cell in column 1 then focus is sent to the cell in column 2 in the same row for example.

I have already set columns 1 & 3 as readonly but as you know it is still possible to click into those cells and hence for those cells to get focus which is what I want to avoid.

I'd settle for taking the cells in columns 1 & 3 out of the tab order but I can't do that either.

Mike
 
I tried handling the CellEnter or CurrentCellChanged events and setting the CurrentCell property if the current column index matched the ones you don't want to focus but it threw an exception because it said it caused an illegal recursive call. I tried another way and, while it's a bit of a hack, it works as long as you have the StandardTab property set to the default False:
VB.NET:
Private Sub DataGridView1_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellChanged
    Dim colIndex As Integer = Me.DataGridView1.CurrentCellAddress.X

    If colIndex = 1 OrElse colIndex = 3 Then
        SendKeys.Send("{TAB}")
    End If
End Sub
 
I got that recursive error which I was surprised by.

Hmm, sendkeys is a bit of a hack and I have a long memeory. I was bitten nastily by that in VB5 I think so I'm not keen to go there. Wasn't there some issue with doevents and the order in which things happened.

It's a pity I can't do it the way I want to but I can cope! The textboxes work it's just very tedious lining them up neatly.

Just out of interest what time zone are you in? I assumed it was the States in which case you must be late to bed or early to rise!

Mike
 
VB.NET is not VB5. SendKeys.Send is reliable in so far as it does what it is supposed to, i.e. simulate a key stroke or key strokes. The issue arises in that in a lot of circumstances you cannot be sure what application or control will have keyboard input focus so you can't be sure how the key stroke(s) will be interpreted. In this case you can be pretty certain that your DataGridView has input focus because it would have to for one of those cells to become selected.

Take a look at the top right corner of one of my posts.
 
As I say I have a long memory and also I'm quite resentful when I have been mauled. But you're right in this case I can be totally confident that the relevant form/control has focus so nothing 'interesting' should happen.

I think I'll take another look. Actually I'm slightly surprised that sendkeys hasn't been dropped as it is something of a kludge and I'm sure best avoided.

Yes, OK I'm an idiot. Sydney - that makes it early evening I guess so it's still civilised.

Mike
 
SendKeys is pretty useful in some situations. For the whole datagridview thing, what you want to do is really easy isnt it? you dont HAVE to use sendkeys either (especially if you ever disabled tabbing). In the currentcellchanged event (focus event possibly) just programmitcally set the focus to the next cell. There are plenty of workarounds for this, and sendkeys is the easiest option
 
UncleRonin said:
SendKeys is pretty useful in some situations. For the whole datagridview thing, what you want to do is really easy isnt it? you dont HAVE to use sendkeys either (especially if you ever disabled tabbing). In the currentcellchanged event (focus event possibly) just programmitcally set the focus to the next cell. There are plenty of workarounds for this, and sendkeys is the easiest option
That's what I thought, but if you set the CurrentCell property explicitly in the CellEnter or CurrentCellChanged event handler you get an exception thrown because of an illegal recursive call. I said that a couple of posts ago and drmike said he experienced the same thing.
 
Back
Top