Solved

FindControl for Gridview for a row being edited..

Posted on 2010-11-29
11
709 Views
Last Modified: 2012-05-10
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..


0
Comment
Question by:moosetracker
11 Comments
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34233028
Hi Moose,

Have you seen this?

http://www.codeproject.com/KB/webforms/EditGridviewCells.aspx

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.

GMM
0
 
LVL 1

Author Comment

by:moosetracker
ID: 34234082
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)
GridviewExample2.jpg
0
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34234255
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?

GMM
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 16

Assisted Solution

by:BurnieP
BurnieP earned 150 total points
ID: 34234847
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();
        }
0
 
LVL 1

Author Comment

by:moosetracker
ID: 34238863
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
0
 
LVL 1

Author Comment

by:moosetracker
ID: 34238923
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...
0
 
LVL 5

Accepted Solution

by:
gingermoleman earned 350 total points
ID: 34239218
moose,
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
0
 
LVL 1

Assisted Solution

by:moosetracker
moosetracker earned 0 total points
ID: 34242649
gingermoleman,
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);
            gvDistributionList.DataBind();
        }
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 34247669
Hello,

Try this code.

Gridview.selectedrow.FindControl["ControlName"];
0
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34259640
Good spot, understand what you mean about consistency.
Glad you fixed it
0
 
LVL 1

Author Closing Comment

by:moosetracker
ID: 34276779
Thanks guys..
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question