[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 932
  • Last Modified:

Set a gridview row in edit mode if it was just inserted from a details view

I have a detailsview and a gridview. I insert a record using the detailsview, and return the identity ID. As soon as the row is inserted, the detailsview is hidden and the gridview is displayed. I want the row that I just added to be in edit mode in the gridview. My gridview uses paging, so if the record I just added is not on the first page I want to make sure the correct page is displaying in the gridview. The purpose is so when the user adds a new record they immediately see the record they just added within the gridview.
0
CitySec
Asked:
CitySec
  • 3
  • 2
1 Solution
 
GreymanMSCCommented:
Which part are you having trouble with?
        <asp:SqlDataSource ID=Sourcerous runat=server ConnectionString="<%$ ConnectionStrings:DevelopmentConnectionString %>"
            SelectCommand="
                SELECT ID,DataKey,DataValue 
                FROM KeyValues WHERE 1=0;"
            InsertCommand="
                BEGIN TRY 
                    BEGIN TRANSACTION; 
                        INSERT INTO KeyValues(DataKey,DataValue) 
                        SELECT @DataKey,@DataValue; 
                    COMMIT TRANSACTION; 
                END TRY 
                BEGIN CATCH 
                    IF (XACT_STATE() = -1) 
                        ROLLBACK TRANSACTION
                END CATCH;"
            ConflictDetection=OverwriteChanges 
            >
            <InsertParameters>
                <asp:Parameter Name=DataKey /><asp:Parameter Name=DataValue />
            </InsertParameters>
        </asp:SqlDataSource>
        <asp:DetailsView ID=Devious runat=server DataSourceID="Sourcerous" DataKeyNames="ID" DefaultMode="Insert" >
            <Fields>
                <asp:BoundField DataField="DataKey" />
                <asp:BoundField DataField="DataValue" />
                <asp:CommandField ShowInsertButton=True />
            </Fields>
        </asp:DetailsView>
 
---
 
    Protected Sub Devious_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles Devious.ItemInserted
        Dim oResult As Object = Nothing
        Dim oConnectionStringSettings As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("DevelopmentConnectionString")
        Using oConnection As New Data.SqlClient.SqlConnection(oConnectionStringSettings.ConnectionString)
            Dim sCommandText As String = "select ID from KeyValues where Id=ident_current('KeyValues');"
            Using oCommand As New Data.SqlClient.SqlCommand(sCommandText, oConnection)
                Try
                    oConnection.Open()
                    oResult = oCommand.ExecuteScalar()
                Catch ex As Exception
                Finally
                    If Not oConnection.State = Data.ConnectionState.Closed Then
                        oConnection.Close()
                    End If
                End Try
            End Using
        End Using
        If oResult Is Nothing OrElse oResult Is DBNull.Value Then
            '--the latest transaction failed so maybe put some error handling here.
        Else
            '-- Now databind the gridview use oResult to locate the row.
        End If
    End Sub

Open in new window

0
 
GreymanMSCCommented:
Once you have the ID, something like this should work.
    Private Sub FindPageToViewID(ByRef ID As Int64)
        Dim bFound As Boolean = False
        Me.GridViewKeyValues.DataBind()
        For Each oGridViewRow As GridViewRow In Me.GridViewKeyValues.Rows
            Using oHiddenField As HiddenField = oGridViewRow.FindControl("HiddenField_ID")
                If oHiddenField IsNot Nothing AndAlso  oHiddenField.Value IsNot Nothing Then
                    If oHiddenField.Value.Equals(ID.ToString) Then
                        bFound = True
                        Exit For
                    End If
                End If
            End Using
        Next
        If Not bFound Then
            Dim iPageIndex As Integer = Me.GridViewKeyValues.PageIndex
            For iIndex As Integer = 0 To Me.GridViewKeyValues.PageCount - 1
                Me.GridViewKeyValues.PageIndex = iIndex
                Me.GridViewKeyValues.DataBind()
                For Each oGridViewRow As GridViewRow In Me.GridViewKeyValues.Rows
                    Using oHiddenField As TextBox = oGridViewRow.FindControl("HiddenField_ID")
                        If oHiddenField IsNot Nothing AndAlso  oHiddenField.Value IsNot Nothing Then
                            If oHiddenField.Value.Equals(ID.ToString) Then
                                iPageIndex = iIndex
                                bFound = True
                                Exit For
                            End If
                        End If
                    End Using
                Next
                If bFound Then Exit For
            Next
            If Not bFound Then
                Me.GridViewKeyValues.PageIndex = iPageIndex
            End If
        End If
        Me.GridViewKeyValues.EditIndex = -1
        Me.GridViewKeyValues.DataBind()
    End Sub

Open in new window

0
 
CitySecAuthor Commented:
Thanks, I have implemented this function and it works, but there are 2 problems.

1) Databinding for every single page is cumbersome on my application. It takes 4 seconds to databind the gridview, and it has around 200 pages, so if I have to loop all the way through to page 200 and databind for every page, i experience an ajax timeout error after about a minute. Can you think of a way to accomplish this without databinding for every page?

2) It finds the page correctly, but it doesn't put the row into edit mode.

Thanks for your help!
0
 
GreymanMSCCommented:
To put the row in edit mode you find the row index, as well as the page index.

Yes, this code would become inefficient for large datasets.  To avoid this you would probably be better off investigating a dataset built with the same SelectCommand, and using that to determine the expected row and page index.

Possibly something like this:
Private Sub GetRowAndPageIndexForID(ByVal ID As Int64, ByRef PageIndex As Integer, ByRef RowIndex As Integer)
    Dim bFound As Boolean = False
    Dim iIndex As Integer = -1
    Dim oSqlDatasource As SqlDatasource = Me.SqlDataSourceKeyValues 'or whatever
    Using oConnection As New Data.SqlClient.SqlConnection(oSqlDatasource.ConnectionString)
        Using oCommand As New Data.SqlClient.SqlCommand(oSqlDatasource.SelectCommand, oConnection)
            Try
                oConnection.Open()
                Using oDataReader As Data.SqlClient.SqlDataReader = oCommand.ExecuteReader()
                    Do While Not bFound AndAlso oDataReader.Read()
                        iIndex += 1
                        If (oDataReader.Item("ID") = ID) Then
                            bFound = True
                        End If
                    Loop
                    oDataReader.Close
                End Using 'oDataReader
            Catch oException As Exception
                '<!-- do exception handling -->
            Finally
                oConnection.Close()
            End Try
        End Using 'oCommand
    End Using 'oConnection 
    If bFound Then
        Dim oGridView As GridView = Me.GridViewKeyValues
        oGridView.PageIndex = iIndex \ oGridView.PageSize
        oGridView.EditIndex = iIndex Mod oGridView.PageSize
        oGridView.DataBind()
    End If
End Sub 

Open in new window

0
 
CitySecAuthor Commented:
That is an excellent solution!

For anyone else who needs this, I'll just clarify exactly what I did. I actually used that code without having to pass pageindex or rowindex. Below is the code that worked for me. I call this in the sqldetailsview_inserted event fyi.
    Private Sub GetRowAndPageIndexForID(ByVal ID As Int64)
        Dim bFound As Boolean = False
        Dim iIndex As Integer = -1
        Dim oSqlDatasource As SqlDataSource = Me.sqlGridview 'or whatever
        Using oConnection As New SqlConnection(oSqlDatasource.ConnectionString)
            Using oCommand As New SqlCommand(oSqlDatasource.SelectCommand, oConnection)
                Try
                    oConnection.Open()
                    Using oDataReader As SqlDataReader = oCommand.ExecuteReader()
                        Do While Not bFound AndAlso oDataReader.Read()
                            iIndex += 1
                            If (oDataReader.Item("ID") = ID) Then
                                bFound = True
                            End If
                        Loop
                        oDataReader.Close()
                    End Using 'oDataReader
                Catch oException As Exception
                    '<!-- do exception handling -->
                Finally
                    oConnection.Close()
                End Try
            End Using 'oCommand
        End Using 'oConnection 
        If bFound Then
            Dim oGridView As GridView = Me.gvMuniData
            oGridView.PageIndex = iIndex \ oGridView.PageSize
            oGridView.EditIndex = iIndex Mod oGridView.PageSize
            oGridView.DataBind()
        End If
    End Sub

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now