Datagridview - 编辑选定的行?

时间:2016-06-03 15:10:07

标签: vb.net datagridview

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

编辑:

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

2 个答案:

答案 0 :(得分:2)

您无法将网格的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

答案 1 :(得分:0)

这是有效的,我只是稍微改变了你的建议(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

感谢你的帮助Lars!