Hello, I'm new to ASP.NET.
I'm requesting help. I've tried hard but with no result.
I want to provide my user a Gridview filled with data from database, allowing him to edit these records not in order to update the database, but in order to insert edited rows in db as new records through a stored procedure.
So I have my Gridview with DataSource set to a DataTable on page Load event
:

gvAutorizzazioni.DataSourceID = ""
        gvAutorizzazioni.DataSource = ds.Tables(0)

with textboxes and ddlists. I put them in ItemTemplate (not in EditItemTemplate) and added a CommandField:

<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkCreaAutorizz" runat="server" CommandName="CreaAutorizz" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
>Crea autorizzazione</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>

In the Click event of the button I want to retrieve current values of the controls, as displayed on Web page after user's actions, and pass them to my sp as a comma-separated string.
The problem is: when I retrieve data using GridView.FindControl, I only get unedited values, those initially displayed from database, not the actual values displayed on screen.

Protected Sub gvAutorizzazioni_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvAutorizzazioni.RowCommand
If (e.CommandName = "CreaAutorizz") Then
            Dim strPerInsert As String = ""
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = gvAutorizzazioni.Rows(index)
            Dim strAdd As String = ""
            strAdd = (CType(row.FindControl("ddlStrutture"), DropDownList).SelectedItem.Value)
            If String.IsNullOrEmpty(strAdd) = False Then strPerInsert = strPerInsert & strAdd & ","
'...and so on to add values to string

What to do to retrieve edited values? Thank you for any help!
(I wanted to avoid - if it's possible! - to use Edit built-in capabilities (Edit button, EditItemTemplate etc.) because I don't need to make any real updating and I don't have a clear idea of how to "bypass" the parts I don't want.)
Cri

Hello, I'm new to ASP.NET.
I'm requesting help. I've tried hard but with no result.
I want to provide my user a Gridview filled with data from database, allowing him to edit these records not in order to update the database, but in order to insert edited rows in db as new records through a stored procedure.
So I have my Gridview with DataSource set to a DataTable on page Load event
:

gvAutorizzazioni.DataSourceID = ""
        gvAutorizzazioni.DataSource = ds.Tables(0)

with textboxes and ddlists. I put them in ItemTemplate (not in EditItemTemplate) and added a CommandField:

<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkCreaAutorizz" runat="server" CommandName="CreaAutorizz" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
>Crea autorizzazione</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>

In the Click event of the button I want to retrieve current values of the controls, as displayed on Web page after user's actions, and pass them to my sp as a comma-separated string.
The problem is: when I retrieve data using GridView.FindControl, I only get unedited values, those initially displayed from database, not the actual values displayed on screen.

Protected Sub gvAutorizzazioni_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvAutorizzazioni.RowCommand
If (e.CommandName = "CreaAutorizz") Then
            Dim strPerInsert As String = ""
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = gvAutorizzazioni.Rows(index)
            Dim strAdd As String = ""
            strAdd = (CType(row.FindControl("ddlStrutture"), DropDownList).SelectedItem.Value)
            If String.IsNullOrEmpty(strAdd) = False Then strPerInsert = strPerInsert & strAdd & ","
'...and so on to add values to string

What to do to retrieve edited values? Thank you for any help!
(I wanted to avoid - if it's possible! - to use Edit built-in capabilities (Edit button, EditItemTemplate etc.) because I don't need to make any real updating and I don't have a clear idea of how to "bypass" the parts I don't want.)
Cri

Hi friend

Just try this

Dim row As GridViewRow  = DirectCast(gvAutorizzazioni.Rows(gvAutorizzazioni.EditIndex), GridViewRow)

instead of

Dim row As GridViewRow = gvAutorizzazioni.Rows(index)

Mark as solved if it helps you!!!

Hi friend

Just try this

Dim row As GridViewRow  = DirectCast(gvAutorizzazioni.Rows(gvAutorizzazioni.EditIndex), GridViewRow)

instead of

Dim row As GridViewRow = gvAutorizzazioni.Rows(index)

Thanks, yousuf... I tried but it doesn't work. I think EditIndex is nothing, because the whole Edit thing is never raised. I also tried setting EditIndex to the index of "pseudo-edited" row, just before your code:

gvAutorizzazioni.EditIndex = index
Dim row As GridViewRow = DirectCast(gvAutorizzazioni.Rows(index), GridViewRow)

But I'm still getting db values :(

Thanks, yousuf... I tried but it doesn't work. I think EditIndex is nothing, because the whole Edit thing is never raised. I also tried setting EditIndex to the index of "pseudo-edited" row, just before your code:

gvAutorizzazioni.EditIndex = index
Dim row As GridViewRow = DirectCast(gvAutorizzazioni.Rows(index), GridViewRow)

But I'm still getting db values :(

Hi Cris651

If you dont raise gridviews edit event , the editindex will always be -1 and therefore you will get the old bound values of grid cells.
To make grid editable. pls use the sample code

Private Sub gvTest_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvTest.RowEditing
         ds = GetData() '// get your data here from database 
        With gvTest
            .EditIndex = e.NewEditIndex
            .DataSource = ds
            .DataBind()
        End With
    End Sub

And then you use the code to find the value of current values in gridviews controls

dim mytextvalue as Textbox 
mytextvalue  = CType(gvTest.Rows(e.NewEditIndex).FindControl("Textboxname"), Textbox)

Pls. mark as solved if it helps you!!!

If you dont raise gridviews edit event , the editindex will always be -1 and therefore you will get the old bound values of grid cells.
To make grid editable. pls use the sample code

Private Sub gvTest_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvTest.RowEditing
         ds = GetData() '// get your data here from database 
        With gvTest
            .EditIndex = e.NewEditIndex
            .DataSource = ds
            .DataBind()
        End With
    End Sub

And then you use the code to find the value of current values in gridviews controls

dim mytextvalue as Textbox 
mytextvalue  = CType(gvTest.Rows(e.NewEditIndex).FindControl("Textboxname"), Textbox)

Thank you again!! I used your code in RowEditing event and gridview becomes editable, but I still have some trouble in accessing the EditTemplate controls... :icon_rolleyes: I'll fight a little more on my own and I'll post the results. :icon_smile:
Cri

I found this and it seems to work now:
- I abandoned the use of a DataTable as source of my Gridview (gvAutorizzazioni)
- I bound gvAutorizzazioni to a SQLDataSource, writing some fake UPDATE statement for the SQLDataSource in order not to receive exceptions OnUpdating;
- on gvAutorizzazioni.OnRowEditing I wrote

gvAutorizzazioni.EditIndex = e.NewEditIndex

as explained by yousuf, to put the gv in edit mode;
- on gvAutorizzazioni.OnRowUpdating I can now retrieve new values using "NewValues" property for each field I need, then cancel updating and use new values for my purposes (here, build a string to use in a sp to insert a new record based on new values):

Protected Sub gvAutorizzazioni_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvAutorizzazioni.RowUpdating
dim strAdd as string = ""
strAdd = e.NewValues("IDStrutturaRichiedente")
' ...(various checks and actions)
e.cancel = true

It does work!! Thank you all for precious help:icon_cheesygrin:
Cri

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.