Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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.

 

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

wayne

 

The Road Goes on Forever and the Party Never Ends - Robert Earl Keen

Posted

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?

Posted

Try a small modification of wayne's code:

 

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.

 

:)

Please check the Knowledge Base before you post.

"Computers are useless. They can only give you answers." - Pablo Picasso

The Code Net

Posted

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

Posted

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.

 

:)

Please check the Knowledge Base before you post.

"Computers are useless. They can only give you answers." - Pablo Picasso

The Code Net

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...