Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make a DataGrid cell on a selective row readOnly

How-to

Make a DataGrid cell on a selective row readOnly

by  Kris11  Posted    (Edited  )
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
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top