[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

FindControl for Gridview for a row being edited..

Posted on 2010-11-29
11
Medium Priority
?
722 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Assisted Solution

by:BurnieP
BurnieP earned 600 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 1400 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

656 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