Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# Gridview -- How Can I Set Row Focus to the next line in the gridview after updating??

Posted on 2010-08-26
7
Medium Priority
?
4,061 Views
Last Modified: 2012-05-10
Experts,
I have a C# (.NET - 3.5 Framework) gridview that does EVERYTHING I want, almost ...

I cannot for the life of me figure out how to set the focus to the Next row after a user updates/cancels an update on a row. It just goes back to the top of the gridview... very frustrating.

I can set the focus to the correct line when I edit the gridview (but it shows up at the very bottom of the page). I would like to know how to make it so that the line I am editing in my gridview shows as the first Line in the gridview.

Here is my code to set the focus when editing (qtyOct is an asp:TextBox in an editItemTemplate within the gridview (this works perfectly):
           ((TextBox)gridProg.Rows[e.NewEditIndex].FindControl("qtyOct")).Focus();

 but the same code construct does not work when I try to set the focus after updating the gridview (see attached code) it does not set the focus to the row immediately after the row just updated, it just goes back to the top of the gridview:

In Summary: 2 Questions (which are practially identical):
How can I set the line I am editing to the top of the gridview?
How can I set the line I have just editied to the top of the gridview?

Any help would be greatly appreciated!! Gotta get this into production tonight and this is not crucial, but one could say neither is having dignity ;-)
Thanks one and all!
Dan
         
protected void gridProg_OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        int NextRow = e.RowIndex; //Go to the next row after canceling the update
        gridProg.EditIndex = -1;
        LoadData();
//The following line "should" set the focus to the next line in the gridview -- Instead the gridview loads to the top of the page.
        ((Label)gridProg.Rows[NextRow].FindControl("lblST")).Focus();

    }

Open in new window

0
Comment
Question by:dantheanswerman
  • 4
  • 2
7 Comments
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33538653
You should have any control like textbox, button in next line, so that you can give focus on that.

After updating line a gridview row, you can set the focus on next row using .focus() method of that that control.

I hope you know how you can access the control inside the gridview.
0
 
LVL 2

Author Comment

by:dantheanswerman
ID: 33538750
Thanks for the comment VjSoft

Can you give me an example with syntax -- the following syntax works in Edit mode - Not in view mode:

((Label)gridProg.Rows[NextRow].FindControl("lblST")).Focus();

0
 
LVL 8

Accepted Solution

by:
Mohit Vijay earned 1500 total points
ID: 33538767
focus on label will not work.

try with a textbox.

((TextBox)gridProg.Rows[NextRow].findcontrol("txtST")).focus();

if you dont want to show textbox on grid, set textbox height to 1 px, backgroud to transparent and its border as flat.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33538784
I hope you have links (edit/update/cancel) with each row, you can set focus on any of these links also, whichever is visible in view mode.
0
 
LVL 2

Author Closing Comment

by:dantheanswerman
ID: 33575803
Thanks for the solution, but I still do not know how to put the selected row in the gridview at the top of the screen. I will open a new question about that.
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33575895
focus on links (if any) not working?
0
 

Expert Comment

by:FrankRam
ID: 37496735

Hello Experts

I am getting this

Compiler Error Message: CS0103: The name 'NextRow' does not exist in the current context

when running this code



0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

886 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