Charlie Stuart
Charlie Stuart

Reputation: 282

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?

Upvotes: 3

Views: 19450

Answers (4)

Sauce
Sauce

Reputation: 35

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

Upvotes: 2

Raj Kumar
Raj Kumar

Reputation: 61

 dataGrid.Columns(index).ReadOnly = True
 dataGrid.Columns(index).ReadOnly = True

 dataGrid.Columns("column_name").ReadOnly = True

Upvotes: 0

Fendy Plick
Fendy Plick

Reputation: 35

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      datagrdSnippets.Columns(0).ReadOnly = True
      datagrdSnippets.Columns(1).ReadOnly = True
      datagrdSnippets.Columns(2).ReadOnly = True
End Sub

Upvotes: 1

senthilkumar2185
senthilkumar2185

Reputation: 2578

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

Upvotes: 4

Related Questions