Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / January 2006

Tip: Looking for answers? Try searching our database.

Catch key press in a datagrid

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rob Oldfield - 25 Sep 2003 17:07 GMT
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.

Signature

Rob Oldfield
www.realuk.co.uk

Ben Geers - 25 Sep 2003 19:20 GMT
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.
>Hopefully, I'm just doing something very silly.
Rob Oldfield - 26 Sep 2003 11:17 GMT
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.

Signature

Rob Oldfield
www.realuk.co.uk

> I was able to do something like this by creating my own
> column style and overriding the Edit method.  See below:
[quoted text clipped - 36 lines]
> DataGrid_KeyDown/Up/Press events.
> >Hopefully, I'm just doing something very silly.
Ben Geers - 26 Sep 2003 15:12 GMT
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

>-----Original Message-----
>Thanks for the idea Ben.  I can see generally how you're trying to get
[quoted text clipped - 43 lines]
>
>.
Rob Oldfield - 26 Sep 2003 21:24 GMT
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.)

Signature

For real reply address, lose the cash
www.realuk.co.uk

> 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
[quoted text clipped - 40 lines]
> > DataGrid_KeyDown/Up/Press events.
> > >Hopefully, I'm just doing something very silly.
csharpnovice - 26 Jan 2006 02:22 GMT
col = new DataGridTextBoxColumn();
col.TextBox.KeyDown +=  event handler
or
col.TextBox.KeyPress +=  event handler

I don't know why but KeyUp does not work

--
csharpnovic

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.