Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid: Disallow Row Editing based on Value of Cell

Status
Not open for further replies.

ISPrincess

Programmer
Feb 22, 2002
318
US
I need to disallow editing on a datagrid based on some value in one of the cells. Here is some background.

I have a datagrid, that has edit and delete buttons for each row. When the click the Edit, the column changes to Update / Cancel buttons. Everything so far is fine with that.

One of the cells in the table (named "InUse") has a value 0 or 1.

If that cells value is 1 then in the ItemDataBound event, I change the backcolor to red (thanks to posts in this forum): like so.

Code:
Private Sub dgPrinters_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgPrinters.ItemDataBound
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
    'If Printer is in use then color the row Red
    'Note that the column to be evaluated must be a 
    '     regular bound column and not an itemtemplate col.
            If e.Item.Cells(4).Text = "1" Then
                e.Item.BackColor = System.Drawing.Color.Red
            End If
        End If
    End Sub

Now, my problem is that when the cell "InUse" is marked 1 and the row is colored Red - I do not want to allow any editing or deletion.

I am not sure how to do this.

It would be great if i could program it to be a client side message stating - cannot edit! or cannot delete! when they click and then do not open the row (edititemtemplate) for editing and do not fire the delete routines.

I have searched for posts on this and have gone thru my books but havent found anything.

Thanks!




PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
You could do the following:

If e.Item.Cells(4).Text = "1" Then
e.Item.BackColor = System.Drawing.Color.Red
e.Item.Cells(0).Enabled = False
End If

Where .Cells(#) is the cell that has the editing link.

HTH

Eva
 
It's not too difficult to implement a client-side JavaScript dialog that would end up cancelling the postback (using Attributes.Add("onclick", "function();") if Cells(4) is "1" or whatever). I have the code at work. Drop another note if you want more on this...

As far as the server side (if the PostBack sneaks through) you should just be able to decide not to set the EditItemIndex based on the Cells(4) value.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top