【问题标题】:How do I allow only 1 col in datagridview editable如何在 datagridview 中只允许 1 列可编辑
【发布时间】:2019-05-15 14:33:14
【问题描述】:

我只想启用 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

【问题讨论】:

  • I am not directly linking to datasource。但是你正在设置一个数据源。
  • 看看这样的事情是否可行:dataGridView1.Columns.OfType(Of DataGridViewColumn)().ToList().ForEach(Function(c) c.ReadOnly = True) dataGridView1.Columns(4).ReadOnly = False

标签: vb.net visual-studio winforms


【解决方案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

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

【讨论】:

  • 这是什么 DgvCol??
  • 它是 DataGridView 中 DataGridViewColumn 的变量赋值
  • Case "Col1","Col2" 'Can Edit 为这个case写什么??
  • @tejasgupta 您在 DataGridView 中写下您不想编辑的列的名称,否则将是只读的。
  • 而且,dgvCol 对我来说将是 table_class.table??
【解决方案2】:
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 

【讨论】:

    猜你喜欢
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多