Contact US

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.

Students Click Here

Visual Basic (Classic) FAQ


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


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)

        'Required for Windows.Forms Class Composition Designer support
    End Sub

    Public Sub New()

        'This call is required by the Component Designer.

        '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
            End If
        End If
    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
                .Parent = TextBox.Parent

                .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
                .Select(0, 0)
            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

            End With

            'Add grid style to the grid


Back to Visual Basic (Classic) FAQ Index
Back to Visual Basic (Classic) Forum

My Archive

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