FindControl for Gridview for a row being edited..

I have been trying.. But not much success

I have a datagrid view where I have made the columns templates. So I have a textbox that I want to read when I select edit, in order to get the origial field as the field I am changing is a key field.. So I can not use the edit logic. When they choose update I need to know the original value in order to delete the record.. And then the new value in order to add the value..

I have tried at the gridview_SelectedIndexChange using
GridViewRow row = gvDistributionList.Rows[e.NewSelectedIndex];
For some reason this event doesn't trigger, or does not trigger befor the RowEditing Event.

At rowEditing Event I have tried the following:

GridViewRow row = gvDistributionList.Rows[e.NewEditIndex];

The field remains null..

So how can I figure out and set programically the row I am working with when the Edit is triggered..

Who is Participating?
gingermolemanConnect With a Mentor Commented:
I'll try to be a bit clearer.

My alternate suggestion is to NOT edit in the grid itself, rather to work around it.
Please see the attached screen shot.

Anyway, view 1 is how the page would be when 1st viewed. The edit fields are always there, they're just empty at this point. Would suggest actually making them inactive.

The ID column in the grid is actually a hyperlink column and is set in the page like this:

<asp:HyperLinkField DataNavigateUrlFields="DBID" DataNavigateUrlFormatString="/yourpath/samepage.aspx?CID={0}" Text="Edit" />

When clicked on, these cause a postback to the same page but now with ?CID=X where x is the ID of the data.

In the page load function you can detect the postback ( if (!IsPostBack)) and then populate these fields when that statement is true.

You would need this to grab the passed through ID

int myID = 0;
myID = Request.QueryString("CID");

and from this you can query your data, delete the record and insert new.

GMM  view 1 view 2
Hi Moose,

Have you seen this?

Details how to create an editable gridview (ie click on the data you want to change, it changes from label to textbox so you can change it). Great if you data source is small.

moosetrackerAuthor Commented:
Ok using what you gave me I got this to try.. But it did not work.
This event fires first..
        protected void gvDistributionList_RowCommand(object sender, GridViewCommandEventArgs e)

            Cache["GridRowko"] = gvDistributionList.Rows[int.Parse(e.CommandArgument.ToString())];


This one fires second but the gridview that comes from the Cache["GridRowko"] is null..       protected void gvDistributionList_RowEditing(object sender, GridViewEditEventArgs e)

            GridViewRow row = (GridViewRow)Cache["GridRowko"];//gvDistributionList.Rows[e.NewEditIndex];//gvDistributionList.SelectedRow;//(GridViewRow)Cache["GridRowko"];

                Cache["origRefKey"] = ((TextBox)row.FindControl("txtRefName")).Text;
                string orgkey = Cache["origRefKey"].ToString();

(See attachement for error)
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Lets not over complicate if we dont have to.

Can you not create a hyperlink column and pass the id back to the page that way for identification?
Would mean creating a form to contain the fields
in your code you would have

If page.ispostback then

//Get the querystring
//setup sql connection
//select myData from myTable Where ID = QrystringID

Does this make sense to you?

BurniePConnect With a Mentor Commented:
I made some modifications to your code, see if you get better results.

        protected void gvDistributionList_RowCommand(object sender, GridViewCommandEventArgs e)
            GridViewRow oRow = gvDistributionList.Rows[int.Parse(e.CommandArgument.ToString())];
            Cache["GridRowko"] = ((TextBox)oRow.FindControl("txtRefName")).Text;

 protected void gvDistributionList_RowEditing(object sender, GridViewEditEventArgs e)
                string orgkey = Cache["GridRowko"].ToString();
moosetrackerAuthor Commented:
BurnieP: I was hopeful, but the same error was there, only on the RowCommand Event..

I posted the Error.. Also the DataGridView of the template for this field, in case anyone can see why I my code thinks I have a null field..  

ErrorTemplate column
moosetrackerAuthor Commented:
gingermoleman - I was confused by your suggestion.  I need a textfield in order that the user can edit the field..  I need to figure out which row on the Datagride is being edited. then first pull into the code the original value (before changes are made).. After the user makes the changes and chooses to update I will need to take that value also..  Then since it is a key, the update of the sqlDataSouce will not update it, so I have to delete out the original record, and add the new record..

I am unsure where the hyperlink will have the original value of the specific row of the grid, or know the altered value the user entered while in Edit Mode...
moosetrackerConnect With a Mentor Author Commented:
I see what you mean.. That would most likely work. My only problem with it was I have set a standard and have a dozen or more datagrids working the same way. This is the first one that had a update on a key field. Therefore, users would not understand the break with the standard grid processing for one form & one grid..

I'm going to split the points between the two of you. I did finally get it to work.. I found a way to get this to work and have poseted it here... Seems that when in rowEditing, your grid is not set up for the Edit mode, but the display mode.. So you can't see the textbox yet, only the Label..

        protected void gvDistributionList_RowEditing(object sender, GridViewEditEventArgs e)
            // gridview EditIndex will not work here, nor will looking for the Textbox.
            // at this point the column still seems to want to look for the label..
            int irow = e.NewEditIndex;
            Cache["origKey"] = ((Label)gvDistributionList.Rows[irow].Cells[1].FindControl("lblRefName")).Text;

        protected void txtRefName_TextChanged(object sender, EventArgs e)
            int irow = gvDistributionList.EditIndex;

            Cache["NewKey"] = ((TextBox)gvDistributionList.Rows[irow].Cells[1].FindControl("txtRefName")).Text;

        protected void gvDistributionList_RowUpdating(object sender, GridViewUpdateEventArgs e)
            string origKey = Cache["origKey"].ToString();
            string NewKey = Cache["NewKey"].ToString();
            runStoredProcedure("UpdName", NewKey, origKey);
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:

Try this code.

Good spot, understand what you mean about consistency.
Glad you fixed it
moosetrackerAuthor Commented:
Thanks guys..
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.