Reputation: 351
I've seen lots of posts out there for this question, however, they all suggest we use methods like: dgv.CurrentCell
or dgv.Rows[row].Cells[cellno].Selected
. Intellisense finds no such methods.
I have a textbox in a gridview, where I use the OnTextChanged method. Then, in my C# code, I update this quantity, then the grid refreshes automatically, and goes back to the top row, even though we've scrolled down a couple of pages. I've also tried putting in the MaintainScrollPositionOnPostback="true" in the 'Page' section of my .aspx page and that didn't seem to do anything. We're on .Net Framework 4.0.
<asp:GridView ID="gvOrderDetails" runat="server" AlternatingRowStyle-BackColor="#FAFAFA" Width="940px" AutoGenerateColumns="False" AllowSorting="True" OnSorting="SortOrderDetails"
OnRowCommand="gvOrderDetails_RowCommand" EmptyDataText="No Data to Display"
DataKeyNames="STOREORDNUM" HeaderStyle-Height="22px"
onselectedindexchanged="gvOrderDetails_SelectedIndexChanged">
<AlternatingRowStyle BackColor="White" /><EditRowStyle BackColor="#2461BF"/>
<FooterStyle BackColor="LightGray" Font-Bold="False" ForeColor="Black" />
<HeaderStyle BackColor="LightGray" Font-Bold="False" ForeColor="Black" BorderWidth="1px" BorderColor="Gray"/><PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" Height="22px"/>
<Columns>
<asp:BoundField DataField="ITMCD" HeaderText="Item Code" SortExpression="ItemCode" >
</asp:BoundField>
....
<asp:TemplateField HeaderText="Order Qty" SortExpression="OrderQty" >
<ItemTemplate>
<asp:TextBox ID="OrderQty" runat="server" Width="36" MaxLength="5" class="numberinput" AutoPostBack="true" OnTextChanged="buttonUpdateQty_Click" Text='<%# Bind("ORDERQTY") %>' ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In the code behind, I've tried these two things:
gvOrderDetails.Rows[row.RowIndex].FindControl("OrderQty").Focus();
gvOrderDetails.Rows[row.RowIndex].Cells[7].Controls[0].Focus();
Upvotes: 2
Views: 12585
Reputation: 1
Add a handler to SelectedindexChange Event of your grid view
Gridview1.SelectedRow.Focus();
Upvotes: 0
Reputation: 26209
You need to cast the required cotrol cell into Textbox
control and then call the Focus()
method on it.
Try This:
TextBox txtOrderQty = (TextBox) gvOrderDetails.Rows[row.RowIndex].FindControl("OrderQty");
txtOrderQty.Focus();
Upvotes: 2