vb.net 使 DataGridView 中的一列只读
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20468056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making one column in DataGridView read only
提问by Charlie Stuart
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:
在我的程序中,我包含了一个在表单加载时填充的 datagridview。当它第一次加载时,我已将整个表单设置为只读。但是,如果用户然后想要编辑其中的数据,他们可以单击我在表单中包含的编辑按钮,其中包含以下代码:
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?
但是,我不希望将 datagridview 中的一列设为可编辑,我可以使用什么代码来执行此操作?
回答by sk2185
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
在表单加载事件中设置 readonly true 您想要的列
回答by Fendy Plick
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
回答by Raj Kumar
dataGrid.Columns(index).ReadOnly = True
dataGrid.Columns(index).ReadOnly = True
dataGrid.Columns("column_name").ReadOnly = True
回答by Sauce
Me, I have data source from database in my dataGridViewso I use for loop to get the exact column address that I want to make ReadOnly=trueand the rest is ReadOnly=false
我,我有来自数据库的数据源,dataGridView所以我使用 for 循环来获取我想要制作的确切列地址,ReadOnly=true其余的是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

