Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an existing data table called POdatagridview. It is bound with a table in the SQL Server database. What I want to happen is to disable editing of the previous records in my front-end but I can able to add new records in the data table.

I tried disabling edit via Properties but, I couldn't add new records.
VB
Private Sub PODataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles POdDataGridView.CellContentClick
        Me.PODataGridView.AllowUserToAddRows = True
    End Sub

Please Help. Non-computer major here. :)
Posted
Updated 29-Oct-13 2:21am
v2
Comments
jaideepsinh 29-Oct-13 8:13am    
Can you show us what you tried and paste you code here.
Karen Lim Yeung 29-Oct-13 8:17am    
Private Sub PODataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles POdDataGridView.CellContentClick
Me.PODataGridView.AllowUserToAddRows = True
End Sub

1 solution

Check for the Primary key value of the current row. If you are adding a new record then make the primary key value to -1 else original primary key value will be populated in the datatable. So while editing check if the primary key value for the current row is -1 or not. If -1 then allow editing. Else disable editing.
 
Share this answer
 
Comments
Karen Lim Yeung 30-Oct-13 1:04am    
I think this will be an appropriate solution. Problem is I don't know to code the primary key. My primary key is the "No" column, where it is in an auto number.
Karen Lim Yeung 30-Oct-13 1:20am    
I tried this code, but it doesn't work.

For Each col As DataGridTextBoxColumn In PODataGridView.Columns
If No.HeaderText = "-1" Then
PODataGridView.EditMode = True
Else
PODataGridView.EditMode = False
End If
Next
Madhu Nair 30-Oct-13 1:24am    
It shouldn't be headertext it should be either cell value if you are using it in your grid or fetch the primary key value from the datatable for the selected row
Karen Lim Yeung 30-Oct-13 1:38am    
It still doesn't work. :(
If NoDataGridViewTextBoxColumn.HeaderCell.DefaultNewRowValue = "-1" Then
PODataGridView.EditMode = True
Else
PODataGridView.EditMode = False
End If

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900