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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable rows in Gridview 2

Status
Not open for further replies.

dmears1

Technical User
Jun 18, 2003
208
US
I would like disable rows in a gridview when a row is in edit mode. Sample code follows:
Code:
<asp:GridView ID="grdContent" runat="server" AllowPaging="true" AllowSorting="false"
AutoGenerateColumns="false" Width="100%" CssClass="GridLines" GridLines="None"
DataSourceID="objDSContent" DataKeyNames="Content_ID">
					<Columns>
<asp:BoundField DataField="Content_Body" HeaderText="Did You Know" ItemStyle-VerticalAlign="middle"
ControlStyle-Width="98%" />
<asp:CommandField ButtonType="image" HeaderText="Options" ItemStyle-HorizontalAlign="center"
ItemStyle-VerticalAlign="middle" ItemStyle-Width="175px" EditImageUrl="~/images/icons/stories-edit.png"
DeleteImageUrl="~/images/icons/stories-delete.png" UpdateImageUrl="~/images/icons/stories-add.png"
CancelImageUrl="~/images/icons/stories-delete.png" ShowEditButton="true" ShowDeleteButton="true" />
</Columns></asp:GridView>
Code:
Private Sub grdContent_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles grdContent.RowEditing
            Dim cf As CommandField = grdContent.Columns(1)
            cf.ShowCancelButton = False
            cf.ShowDeleteButton = False
            cf.ShowEditButton = False
            cf.ShowInsertButton = False
            For Each row As GridViewRow In grdContent.Rows
                If row.RowIndex = e.NewEditIndex Then
                    cf.ShowCancelButton = True
                    cf.ShowEditButton = True
                End If
            Next
            grdContent.DataBind()
        End Sub
Any help would be greatly appreciated. I would be fine with either hiding the commandfield imagebuttons in the non-edit rows or just disabling them.
 
One possibility may be to convert the column to an template column and disenable - have you carried out a general search at Google Groups - I spent a few minutes there looking at at a couple of approaches to this problem, e.g.,

Disenable edit buttons in Gridview

I'm sure there are a number of ways to accomplish this - just a thought to help you work through this.





 
Use the RowDataBound event of the grid. Use the .FindControl() method to find the edit button, and disable it, if it is not the index of the row that is being edited
 
Thank you both for the help.
Isadore: I have spent half a day googling try to find an answer but can't seem to find exactly what I need. GoogleGroups is something I hadn't look through. Thanks for the link.
jbenson001: I have tried doing this on RowDataBound and actually came close to what I am wanting to accomplish. However, because RowDataBound fires on page load, before the gridview is in edit mode, I couldn't not get it to work correctly.
 
Here's the solution I came up with:

Code:
Private Sub grdContent_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdContent.RowDataBound
            If grdContent.EditIndex <> -1 And e.Row.RowType = DataControlRowType.DataRow And e.Row.RowState <> DataControlRowState.Edit Then
                If e.Row.RowState = DataControlRowState.Alternate Or e.Row.RowState = DataControlRowState.Normal Then
                    e.Row.Cells(1).Controls.Clear()
                End If
            End If
        End Sub

Thanks again for the direction both of you provided.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top