【问题标题】:Datagridview - edit selected row?Datagridview - 编辑选定的行?
【发布时间】:2016-06-03 15:10:07
【问题描述】:

我已将 Datagridview .ReadOnly 属性设置为 True,然后添加了一个按钮列。按钮列用于单击时编辑按钮,但我希​​望仅编辑当前选定的行。这是我尝试过的:

编辑:

Public Class Form2

Private Sub Form2_Resize(sender As Object, e As EventArgs) Handles Me.Resize
    Me.DataGridView1.Height = 0.8 * Me.Height
End Sub

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'Users._USERS' table.                           You can move, or remove it, as needed.
    Me.USERSTableAdapter.Fill(Me.Users._USERS)
    Me.DataGridView1.DefaultCellStyle.Font = New Font("Arial", 7)

 End Sub

Protected Overrides Sub OnLoad(e As EventArgs)
   MyBase.OnLoad(e)
   For i As Integer = 0 To DataGridView1.Rows.Count - 1
   DataGridView1.Rows(i).ReadOnly = True
   Next
End Sub

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

Dim yourColumnIndex As Int32 = 3
If e.ColumnIndex = yourColumnIndex Then

If MsgBox("Do you wish to edit records?", vbQuestion + vbYesNo, "Edit records") = vbYes Then
 DataGridView1.Rows(e.RowIndex).ReadOnly = False

End If
End If

End Sub

Private Sub DataGridView1_RowLeave(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
     DataGridView1.Rows(e.RowIndex).ReadOnly = True
End Sub


 End Class

【问题讨论】:

    标签: vb.net datagridview


    【解决方案1】:

    您不能将网格的 ReadOnly 属性设置为 true。将行添加到网格后,您必须遍历行并为每一行设置 ReadOnly 属性:

    Protected Overrides Sub OnLoad(e As EventArgs)
      MyBase.OnLoad(e)
      For i As Integer = 0 To DataGridView1.Rows.Count - 1
        DataGridView1.Rows(i).ReadOnly = True
      Next
    End Sub
    

    注意:您不能在表单的构造函数中设置这些属性,这是 DataGridView 控件的一个怪癖。

    然后使用e参数提供的RowIndex属性:

    Private Sub DataGridView1_CellContentClick(sender As Object, 
                                               e As DataGridViewCellEventArgs)
                                               Handles DataGridView1.CellContentClick
      If e.ColumnIndex = 3 Then
        DataGridView1.Rows(e.RowIndex).ReadOnly = False
      End If
    End Sub
    

    离开行时设置回true:

    Private Sub DataGridView1_RowLeave(sender As Object,
                                       e As DataGridViewCellEventArgs)
                                       Handles DataGridView1.RowLeave
      DataGridView1.Rows(e.RowIndex).ReadOnly = True
    End Sub
    

    【讨论】:

    • 谢谢拉尔斯,但它不起作用。我已将 DatagridView 控件 .ReadOnly 再次设置为 true。
    • @LuckyLuke82 我发布的代码在我的机器上运行。您必须比“它不起作用”更具描述性。
    • 对不起,我是 vb.net 的初学者,我认为 Overrides Sub 可能有问题。打开表单时,DatagridView 仍可编辑(如果我将属性 .ReadOnly 设置为 False)。如果设置为 True 也同样的结果。我是否必须将此代码放在其他地方,我已经有了 Form_Load 事件?
    • @LuckyLuke82 将您拥有的代码放入您的加载事件中,并放在我提供的 OnLoad 覆盖中。表单不应该监听自己的事件,总是使用覆盖来代替
    • 我不明白。加载事件中的代码是另外一回事......等等,看看我编辑的问题,来自表单的完整代码。
    【解决方案2】:

    这行得通,我只是稍微改变了你的建议(DatagridView Readonly 属性必须设置为 False):

    Protected Overrides Sub OnLoad(e As EventArgs)
            MyBase.OnLoad(e)
            For Each band As DataGridViewBand In DataGridView1.Columns
                band.ReadOnly = True
            Next
    End Sub
    
    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    Dim yourColumnIndex As Int32 = 3
    If e.ColumnIndex = yourColumnIndex Then
       If MsgBox("Do you wish to edit record?", vbQuestion + vbYesNo, "Edit      record") = vbYes Then
       For Each band As DataGridViewBand In DataGridView1.Columns
           band.ReadOnly = False
       Next
       End If
    End If
    End Sub
    
    Private Sub DataGridView1_RowLeave(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
        For Each band As DataGridViewBand In DataGridView1.Columns
            band.ReadOnly = True
        Next
    End Sub
    

    感谢拉尔斯的所有帮助!

    【讨论】:

      猜你喜欢
      • 2017-10-25
      • 1970-01-01
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      相关资源
      最近更新 更多