Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Bound Data Grid View - Inhibit Edit of PK Field.
 

Bound Data Grid View - Inhibit Edit of PK Field.

Not sure how I've managed to avoid this after all of the years of .NET but,....

I have a form with a data grid view on it that I am binding as follows:

    Protected dsRooms As New DataSet

    Protected dvRooms As New DataView

    Private Sub frmRoomEdit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Try

            daRooms.Fill(dsRooms, ROOMS)



            '-- allow for update, new, delete.

            Dim commandbuilder As New SqlCommandBuilder(daRooms)



            dvRooms = dsRooms.Tables(ROOMS).DefaultView

            dvRooms.Sort = "Room ASC"



            dgvRooms.DataSource = dvRooms

        Catch ex As Exception

            MsgBox("Error Loading Room List: " & ex.Message)

        End Try

    End Sub

When I added the dgv to the form I unchecked the options to ADD and DELETE rows.

Now, I am interested in inhibiting the editing of the pk field column.

Is this possible and how to proceed?

TIA - Gary in Massena.


Gary in Massena
Gary in Massena
GTNovosel  Friday, July 17, 2009 8:50 PM
Hi Gary -

Do the users really need to even see these fields? I normally try to just hide the entire key column because the users don't really need to worry about the key that is given to the row.

If that is not possible for you, you can prevent the editing of the cells. There is a read-only property.

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Friday, July 17, 2009 11:28 PM
Hi Gary -

Do the users really need to even see these fields? I normally try to just hide the entire key column because the users don't really need to worry about the key that is given to the row.

If that is not possible for you, you can prevent the editing of the cells. There is a read-only property.

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Friday, July 17, 2009 11:28 PM
Normally I would hide the PK field. In the case of the problem I am working on, the PK field contains human readable information that the user will need to see (just not edit).

One solution that I just began to explore was to, after the dgv was populated, simply browse through the primarykeys collection to isolate the involved columns and then to manually set the dgv columns read only as appropriate.

Still, it seems that there must be an easier way.

Thanks, G


Gary in Massena
GTNovosel  Tuesday, July 21, 2009 1:16 PM
There is actually a ReadOnly property on a column as well:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.readonly.aspx

That may be easier than looping through and setting it for each cell.

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Tuesday, July 21, 2009 3:30 PM

You can use google to search for other answers

Custom Search

More Threads

• Merging of cells in datagridview control
• LDAP databinding.
• Datagridview - generic display
• Change field-Values in Edit-Mode
• set databinding to nothing
• ListBox Data Binding and Selection Set Accessing
• Automatic sizing of column width based on text length
• DataGridView and objectdatasource
• SQL parameter query using INSTR
• Is there a way to bind a property of a row in a datagridview to a column in datasource.