我如何在datagridview中仅允许1 col可编辑

时间:2019-05-15 14:33:14

标签: vb.net visual-studio winforms

我只想启用DataGridview中的两列才能进行编辑。其他不应该被允许编辑。此外,我没有直接链接到数据源。

Public Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        table_class.table.Columns.Add("Subject", Type.GetType("System.String"))
        table_class.table.Columns.Add("Date Assigned", Type.GetType("System.DateTime"))
        table_class.table.Columns.Add("Date of Submission.", Type.GetType("System.DateTime"))
        table_class.table.Columns.Add("Date of Completion", Type.GetType("System.DateTime"))
        table_class.table.Columns.Add("Done", Type.GetType("System.Boolean"))

        DataGridView.ReadOnly = True
        DataGridView.Columns[4].ReadOnly = False

        DataGridView.DataSource = table_class.table

    End Sub

2 个答案:

答案 0 :(得分:1)

删除此:

DataGridView.ReadOnly = True

尝试一下:

        For Each DgvCol As DataGridViewColumn In DGV.Columns
            Select Case DgvCol.Name
                Case "Col1","Col2" 'Can Edit

                Case Else
                    DgvCol.ReadOnly = True 'cant edit
            End Select
        Next

编辑为在第一种情况下包含第二个条件,以显示多个列排除项

答案 1 :(得分:-1)

for i=0 to  DataGridView.Columns.count-1

if i<=1 then 
DataGridView.Columns(i).readonly=false
else
DataGridView.Columns(i).readonly=true
end 

next