Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making one column in DataGridView read only

Within my program i have included a datagridview that is filled when the form loads. When it first loads i have set the whole form to read-only. However if the user then wants to edit the data within it they can click an edit button i have included on the form, this has the code:

datagrdSnippets.AllowUserToDeleteRows = True 'Allows user to delete rows
datagrdSnippets.ReadOnly = False 'Allows user to edit cells within the data grid

However i do not want one of the columns within the datagridview to be made editable, what code can i use to do this?

like image 911
Charlie Stuart Avatar asked Dec 11 '25 09:12

Charlie Stuart


2 Answers

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Column1.ReadOnly = True
        Column2.ReadOnly = True
        Column3.ReadOnly = True
End Sub

set readonly true your desired column on form load event

like image 186
senthilkumar2185 Avatar answered Dec 14 '25 07:12

senthilkumar2185


Me, I have data source from database in my dataGridView so I use for loop to get the exact column address that I want to make ReadOnly=true and the rest is ReadOnly=false

Code:

For i = datagridview1.columns.count - 1 to 0 Step -1

  If i = (YourSpecificColumnAddress) Then
     Datagridview1.columns(i).ReadOnly=true
  Else  
     Datagridview1.columns(i).ReadOnly=false
  End if

Next
like image 29
Sauce Avatar answered Dec 14 '25 06:12

Sauce