Catch key press in a datagrid

R

Rob Oldfield

I have a windows forms data grid where I want to disallow the editing of
certain cells. At the moment though I can't get any code to trigger,
whether I include it in any of the DataGrid_KeyDown/Up/Press events.
Hopefully, I'm just doing something very silly.
 
B

Ben Geers

I was able to do something like this by creating my own
column style and overriding the Edit method. See below:

public class DataGridReadOnlyTextBoxColumn :
DataGridTextBoxColumn
{
private int m_ReadOnlyRow;

public int intReadOnlyRow {
get{return this.m_ReadOnlyRow;}
set{this.m_ReadOnlyRow = value;}
}

//this override will prevent the cell in row
m_ReadOnlyRow from getting the edit focus
protected override void Edit
(System.Windows.Forms.CurrencyManager source, int rowNum,
System.Drawing.Rectangle bounds, bool readOnly, string
instantText, bool cellIsVisible) {

if(rowNum == this.m_ReadOnlyRow)

return;

base.Edit(source, rowNum, bounds,
readOnly, instantText, cellIsVisible);

}
}

Hope this helps.
-----Original Message-----
I have a windows forms data grid where I want to disallow the editing of
certain cells. At the moment though I can't get any code to trigger,
whether I include it in any of the
DataGrid_KeyDown/Up/Press events.
 
R

Rob Oldfield

Thanks for the idea Ben. I can see generally how you're trying to get
around the issue, but sadly I'm a VB type and can't recreate the effect. If
anyone could translate this it'd be much appreciated.
 
B

Ben Geers

This builds, but it's been a while since I coded vb.net so
bear with me.

Imports System.Windows.Forms
Imports System.Drawing

Public Class DataGridReadOnlyTextBoxColumn
Inherits DataGridTextBoxColumn

Dim m_ReadOnlyRow As Integer

Public Property intReadOnlyRow()
Get
intReadOnlyRow = Me.m_ReadOnlyRow
End Get
Set(ByVal Value)
Me.m_ReadOnlyRow = Value
End Set
End Property

Protected Overloads Overrides Sub Edit(ByVal source As
CurrencyManager, ByVal rowNum As Integer, ByVal bounds As
Rectangle, ByVal cellIsVisible As Boolean)
If rowNum = Me.m_ReadOnlyRow Then
Exit Sub
End If

MyBase.Edit(source, rowNum, bounds, cellIsVisible)
End Sub

End Class
 
R

Rob Oldfield

Thanks again Ben. Sadly it's not working for me. I'm adding the column
like this...

Dim ncol As New DataGridReadOnlyTextBoxColumn()
ncol.MappingName = "Broker"
ncol.intReadOnlyRow = 1
Me.DataGrid1.TableStyles(0).GridColumnStyles.Add(ncol)

....and that builds fine and displays the 'Broker' info correctly, but the
Edit event isn't firing. I can edit the broker info in the second row in
this example, and if I add a breakpoint into the Edit code, it never gets
triggered.

One other thing, even if this was working, I still need some more
flexibility. I need to make multiple rows read only depending on other
values in each record (the actual case is I have a column with either Sale
or Purchase - if it's a Sale make the broker editable, if a Purchase then
read only). Going down your suggested road, can I pass the class an array
containing all the row numbers I want to be read only, and will it work if
the user sorts the datagrid?

(Apologies for replying to my own post, but the newsreader I'm using now
isn't showing Ben's second post to this thread. Had to grab it from
Google.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top