INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How-to

Make a DataGrid cell on a selective row readOnly by Kris11
Posted: 22 Mar 04 (Edited 22 Mar 04)

Steps

1. Inherit DataGridTextBoxColumn
2. Overload/Override the Edit sub and put the logic  the way you want it.

Inherited DataGridTextBoxColumn

Public Class clsGridTextBoxColumn
    Inherits DataGridTextBoxColumn

#Region "Declarations"

    Private WithEvents ue As TextBox = New TextBox
#End Region

#Region " Component Designer generated code "

    Public Sub New(ByVal Container As System.ComponentModel.IContainer)
        MyClass.New()

        'Required for Windows.Forms Class Composition Designer support
        Container.Add(Me)
    End Sub

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Component overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Component Designer
    'It can be modified using the Component Designer.
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
    End Sub

#End Region

Edit - This is were you have to put the logic, you can create your on event handlers and add it your form. Here as an example I am making cell editable only if there is a value already present.

#Region "Events"

    Protected Overloads Overrides Sub Edit(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal read_Only As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)

        Dim isReadOnly As Boolean = False
        Dim sValue As String

        'Any cell with NULL value keep it readOnly
        If (IsDBNull(Me.GetColumnValueAtRow(source, rowNum))) Then isReadOnly = True

        MyBase.Edit(source, rowNum, bounds, isReadOnly, instantText, False)

        'Make the cell editable
        If Not isReadOnly Then
            With ue
                .Invalidate()
                .Parent = TextBox.Parent
                .SuspendLayout()

                .Text = Me.GetColumnValueAtRow(source, rowNum)
                .Location = New Point(bounds.X + 2, bounds.Y)
                .Size = New Size(bounds.Width - 2, bounds.Height)
                bounds.Width -= 2

                .Visible = True
                .Focus()
                .Select(0, 0)
                .ResumeLayout()
            End With
        End If
    End Sub
#End Region

End Class


And in your form - create a class of the new type you just created

Dim oColStyle As clsGridTextBoxColumn = New clsGridTextBoxColumn
            With oColStyle
                .MappingName = "column_name"
                .HeaderText = "Header Text"
                .Width = 100
            End With

            'Add column styles to grid style
            With gridTableStyle.GridColumnStyles

                .Add(oColStyle)
            End With

            'Add grid style to the grid
            DataGrid1.TableStyles.Add(gridTableStyle)


-Kris

Back to Visual Basic (Microsoft) VB.NET FAQ Index
Back to Visual Basic (Microsoft) VB.NET Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close