Solved

FindControl for Gridview for a row being edited..

Posted on 2010-11-29
11
683 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now