lorena Posted August 29, 2005 Posted August 29, 2005 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 Quote
clabarca Posted August 29, 2005 Posted August 29, 2005 Have you tried: e.Item.Cells(8).ReadOnly = True Hope it helps, Cesar Quote
lorena Posted August 30, 2005 Author Posted August 30, 2005 I tried setting it to ReadOnly as you suggested but it errors out. Quote
wayneph Posted August 30, 2005 Posted August 30, 2005 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 Quote wayne The Road Goes on Forever and the Party Never Ends - Robert Earl Keen
lorena Posted August 30, 2005 Author Posted August 30, 2005 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? Quote
mark007 Posted August 31, 2005 Posted August 31, 2005 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. :) Quote Please check the Knowledge Base before you post. "Computers are useless. They can only give you answers." - Pablo Picasso The Code Net
lorena Posted August 31, 2005 Author Posted August 31, 2005 Thanks for the idea. I tried it but the Edit event still fires and I still get the "Update" and "Cancel" Buttons Quote
lorena Posted August 31, 2005 Author Posted August 31, 2005 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 Quote
mark007 Posted September 1, 2005 Posted September 1, 2005 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. :) Quote Please check the Knowledge Base before you post. "Computers are useless. They can only give you answers." - Pablo Picasso The Code Net
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.