You can still use the readonly - but you have to code around it a little. In my example I have a button which I can disable further changes to be made and whilst it disallows changes to be made the selected value remains but it allows me to scroll around the grid and the values are readonly - when I reclick the button to reenable selection of rows then editing is re-enabled.
You just have to maintain the selected item you want to preserve and handle this.
Example
Public Class Form1 Dim blnNoChnage As Boolean = False Dim selrow As Integer '//Currently Selected Row
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '//Populate the datagridview Dim dt As New DataTable dt.Columns.Add("1") dt.Columns.Add("2")
dt.Rows.Add(1, 2) dt.Rows.Add(2, 2) dt.Rows.Add(3, 2) dt.Rows.Add(4, 2) Me.DataGridView1.DataSource = dt
Me.DataGridView1.MultiSelect = False Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click blnNoChnage = Not blnNoChnage '//Toggles between allow or not allowing selected row to be changed Me.DataGridView1.ReadOnly = blnNoChnage End Sub
Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged If Me.DataGridView1.SelectedRows.Count > 0 Then '// If no chnage allowed then reset the selected row back to what it should be '// else allow the chnage to take place and update the selrow variable If blnNoChnage = True Then CType(sender, DataGridView).Rows(selrow).Selected = True Else selrow = CType(sender, DataGridView).CurrentRow.Index End If Label1.Text = Me.DataGridView1.SelectedRows(0).Cells(0).Value
End If
End Sub End Class
|