'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?
Solution 1:[1]
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
Solution 2:[2]
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
Solution 3:[3]
dataGrid.Columns(index).ReadOnly = True
dataGrid.Columns(index).ReadOnly = True
dataGrid.Columns("column_name").ReadOnly = True
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kuro Neko |
| Solution 2 | Fendy Plick |
| Solution 3 | Raj Kumar |
