3

I have the need to allow a user to "tab through" making edits on a gridview. There will be one editable column in the row data. The user should be able to hit tab and go to the next row to edit said column.

I have not found any easy method to accomplish this task. I found a way to programmatically put a gridview into edit mode, but in testing the code below it works for only 1 row at a time.

        reviewTransferGV.EditIndex = 0;
        reviewTransferGV.Rows[0].RowState = DataControlRowState.Edit;
        reviewTransferGV.EditIndex = 1;
        reviewTransferGV.Rows[1].RowState = DataControlRowState.Edit;
        reviewTransferGV.DataBind();
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348

3 Answers3

10

I did a workaround by creating a property in the page:

protected bool IsEditMode
{
  get { return this.EditMode; }
  set { this.EditMode = value; }
}

Then in the GridView I have the controls for view and edit mode inside an item template. Setting the visibility based on the property value:

<asp:TemplateField SortExpression="Status" HeaderText="Status">
<ItemTemplate>
    <asp:Label Id="lblStatus" Text='<%# Eval("Status") %>' Visible='<%# !IsEditMode %>' runat="server" />
    <asp:TextBox ID="txtStatus" Text='<%# Eval("Status") %>' Visible='<%# IsEditMode %>' runat="server" />
</ItemTemplate>

This works for editing the whole gridview. You'll probably need to make a few modifications to make it work for individual rows.

jpiolho
  • 1,192
  • 11
  • 12
  • Works like a charm. Interestingly enough, this little trick can also be used to grab column data that you do not want to display. E.G. if you set Visible=False on a boundfield you cannot get to the Text value in codebehind, but if you use a templatefield and set the TemplateField visible=false you can still get to the "lblStatus" Text value and lblStatus will not display. – P.Brian.Mackey Nov 30 '10 at 16:38
  • Hi, @P.Brian.Mackey where to set the property? can you please share code? – Shilpa Soni May 31 '18 at 02:45
0

Another point is how to save the results to the DataBase. While in regular use, we simple call the update command who does the work, in ItemTemplate there are now update button. So i add a button outside the GridView and in the handler i call the UpdateRow method manually for each row.

IFink
  • 724
  • 16
  • 28
0

I don't believe it is possible for a GridView to have multiple rows in edit mode simultaneously. If you want to edit multiple rows, you will need to roll your own mechanism to do so.

John Bledsoe
  • 17,142
  • 5
  • 42
  • 59
  • I kinda figured that, do you have any suggestions as to where to start building my own control/extending gridview to do this? I'm skimming basic control building in a Wrox book in the hopes that the answer will be there. – P.Brian.Mackey Nov 30 '10 at 14:44