asp.netgridvieweditrowstate

ASP.NET GridView Multiple Edit Rows At Same Time


In ASP.NET 4.0 GridView, is it possible to have multiple rows in edit mode at the same time?

I'm controlling the rows with edit mode in a property:

Private Property Editing As List(Of Integer)
   Get
       If ViewState("Editing") Is Nothing Then ViewState("Editing") = New List(Of Integer)
       Return CType(ViewState("Editing"), List(Of Integer))
   End Get
   Set(value As List(Of Integer))
       ViewState("Editing") = value
   End Set
End Property

Populating it when the user clicks on edit button:

Protected Sub GridView1_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
    If e.CommandName = "Edit" Then
        Dim row = CType(CType(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
        Editing.Add(row.RowIndex)
    End If
End Sub

And changing the RowState property manually in the RowDataBound event:

Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        If Editing.Contains(e.Row.RowIndex) Then
            Then e.Row.RowState = DataControlRowState.Edit
        End If
    End If
End Sub

But it's not working, the rows is being rendered in the normal state... any ideias?


EDIT 2: PROPERTY

MultipleEditGridView.vb:

Namespace ClubeCheckIn.UI

        Public Class MultipleEditGridView
            Inherits GridView

            Protected Property IsRowInEditMode(rowIndex As Int32) As Boolean
                Get
                    If ViewState("GridRowEditIndices") Is Nothing Then
                        Return False
                    Else
                        Dim indices = DirectCast(ViewState("GridRowEditIndices"), List(Of Int32))
                        Return indices.Contains(rowIndex)
                    End If
                End Get
                Set(value As Boolean)
                    If ViewState("GridRowEditIndices") Is Nothing Then
                        ViewState("GridRowEditIndices") = New List(Of Int32)
                    End If
                    Dim indices = DirectCast(ViewState("GridRowEditIndices"), List(Of Int32))
                    indices.Remove(rowIndex)
                    indices.Add(rowIndex)
                End Set
            End Property

        End Class

    End Namespace

web.config:

<controls>
    <add tagPrefix="clube" namespace="ClubeCheckIn.UI" />
</controls>

ASPX:

<clube:MultipleEditGridView ID="GridView1" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txtEdit" runat="server" Visible="<%# IsRowInEditMode(Container.DataItemIndex) %>" />
            </ItemTemplate>
    </Columns>
</clube:MultipleEditGridView>

ERROR:

Error: BC30451: 'IsRowInEditMode' is not declared. It can be inacessible due to the protection level


Solution

  • I'm pretty sure that the GridView does not support multiple rows in edit-mode.

    As a work around you can use the ItemTemplate for both states (f.e. a Label and a TextBox). Then you can use a property EditMode with RowIndex as argument. You can store the rows in edit-mode in ViewState.

    (not tested)

    Protected Property IsRowInEditMode(rowIndex As Int32) As Boolean
        Get
            If ViewState("GridRowEditIndices") Is Nothing Then
                Return False
            Else
                Dim indices = DirectCast(ViewState("GridRowEditIndices"), List(Of Int32))
                Return indices.Contains(rowIndex)
            End If
        End Get
        Set(value As Boolean)
            If ViewState("GridRowEditIndices") Is Nothing Then
                ViewState("GridRowEditIndices") = New List(Of Int32)
            End If
            Dim indices = DirectCast(ViewState("GridRowEditIndices"), List(Of Int32))
            indices.Remove(rowIndex)
            indices.Add(rowIndex)
        End Set
    End Property
    

    you could call it directly from the markup, f.e. for the edit-controls:

    Visible='<%# IsRowInEditMode(Container.DataItemIndex) %>