Make datagrid row read only

lorena

Centurion
Joined
Oct 23, 2003
Messages
134
Location
Phoenix, Arizona
I have a form with a datagrid that loads open and closed records for the users to view. They need to be able to edit records that are not closed but closed records should be read only. Is there a way to do this? I thought I had a handle on it but my coe doesn't work.
Here it is:
Sub dgRecs_EditRow(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
dgRecs.EditItemIndex = e.Item.ItemIndex
If IsDate(e.Item.Cells(8).Text) Then
e.Item.Cells(8).Enabled = False
End If
BindData()
End Sub
 
I'd use something like this to prevent the user from clicking edit.

It will clear out the controls from the "Edit" column in the ItemDataBound so that the buttons don't even show up.

Visual Basic:
Private Sub dgRecs_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
      Handles dgRecs.ItemDataBound
  If IsDate(e.Item.Cells(8).Text) Then
    'Replace the index with the column of your Edit Column
    e.Item.Cells(10).Controls.Clear()
  End If
End Sub
 
I tried:
e.Item.Cells(0).Controls.Clear (0 is the Edit index)
and also tried
e.Item.Cells(8).enable = false
and then I thought maybe if I just reset the EditItemIndex
dgRecs.EditItemIndex = -1
but the Edit Item control (in this case a calendar control) is still visible and clickable.
Isn't there some way to just ignore the user request if that particular cell in the row is a date?
 
Try a small modification of wayne's code:

Visual Basic:
Private Sub dgRecs_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
      Handles dgRecs.ItemDataBound
  If IsDate(e.Item.Cells(8).Text) Then
    'Replace the index with the column of your Edit Column
    ctype(e.Item.Cells(0).Controls(0),linkbutton).Visible=false
  End If
End Sub

I would also place a debig point in the code and check the values are as you expect in e.Item.Cells(8).Text for example.

:)
 
I also tried using the ItemCommand because it seems that the Edit fires as soon as it's clicked showing the "Update" and "Cancel" linkbuttons. So I was hoping to disable the Update but this doesn't work either.
Private Sub dgRecs_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgRecs.ItemCommand
Select Case (CType(e.CommandSource, LinkButton)).CommandName
Case "Update"
If e.Item.Cells(8).Text <> "" Then
e.Item.Cells(0).Enabled = False
End If
Case Else
' Do nothing.

End Select
End Sub
 
Firstly - make your code easier to read by using [ vb][/vb ] tags without the spaces to format the code as in my post.

I don't understand how edit could fire on that row as the Edit button should have been hidden.

Have a read of the articles here:

http://aspnet.4guysfromrolla.com/articles/040502-1.aspx

In particular part read part 8 that deals with getting a reference to a control in the databind event. The difference is you want to reference the eidt button and hide it rather than adding client script.

:)
 
Back
Top