June 2nd, 2014, 05:32 AM
Getting data from BoundField by name
For templatefields, I can get data from a textbox in my GwRowUpdating function by
obj.Name = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TxtName"))).Text.Trim();
And I've seen people getting data from BoundFields by
TextBox txtBox = (TextBox)GridView1.Rows[GridView1.EditIndex].Controls;
This works fine as long as you don't change the order of the columns. I would much rather be able to do something like
TextBox txtBox = (TextBox)GridView1.Rows[GridView1.EditIndex].Controls["Name"];
when getting data from
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
But it seems I can only use an index no rather than a text like for instance connection strings
June 2nd, 2014, 12:23 PM
Re: Getting data from BoundField by name
Why not consider a different approach? Rather than reading the data from the columns/rows in the control, why not access the data from the datasource directly? If you are returning table data from a db, create a concrete object to store the data and map the data from the table, and put the object into a collection. Then bind the collection to the grid.
Do all your operations (insertions, deletions, updates, etc.) on the collection and rebind it to the control. Do all your sorting operations on the collection as well.
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.