How to make a specific row in a datagrid to become read only?

  • Thread starter Thread starter ywchan
  • Start date Start date
Y

ywchan

I would like to make some of the rows in the datagrid read only according to
certain condition.
How can I make some of the rows editable while others read only in a
datagrid? Thanks!
 
Hi,

For this, you can trap the row being clicked in the MouseDown event of the grid. Check for row number or its value and set the ReadOnly property of the grid to True:

Private Sub DataGrid1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseDown
Dim htinfo As DataGrid.HitTestInfo

htinfo = Me.DataGrid1.HitTest(e.X, e.Y)

Dim r As Integer

r = htinfo.Row

If r = 2 Then

Me.DataGrid1.ReadOnly = True

Else

Me.DataGrid1.ReadOnly = False

End If

End Sub

However, if you wish to make a row readonly when the grid is loaded, you can inherit from DataGridTextBoxColumn and create a customized Column Style
yourself, then you can override Edit method. Finally determine the value of the current edit cell and disable the edit function. See the following article on implementing a custom column style:

http://msdn.microsoft.com/library/d...windowsformsdatagridcolumnstyleclasstopic.asp

Hope this helps.

Thanks

Mona [GrapeCity]
 
Back
Top