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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 720
  • Last Modified:

Gridview update code

I am updating a gridview using c# code. I am trying to capture the new values from the gridveiw after I click edit... therefore I have been using the following code.

    protected void edit_record_updating(object sender, GridViewUpdateEventArgs e)
    {
       
        update_class update_records_method = new update_class();
        update_records_method.Record_ID = ((TextBox)user_records.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
        update_records_method.Logon_Name = WindowsIdentity.GetCurrent().Name.ToString();
        update_records_method.Record_Class = ((TextBox)user_records.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString();  
        update_records_method.Record_Name = ((TextBox)user_records.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString();
}

The problem is that not text seems to be  being picked up when I entre it into the text boxes
0
Mr_Shaw
Asked:
Mr_Shaw
  • 2
  • 2
1 Solution
 
RamuncikasCommented:
Make sure your textboxes have IDs assigned and try to use this:
update_records_method.Record_ID = ((TextBox)user_records.Rows[e.RowIndex].FindControl("IdTextBox")).Text;

Open in new window

0
 
Mr_ShawAuthor Commented:
How do I create ID's for the textboxes used in the Gridview...They seem to be created dynamicly.
0
 
RamuncikasCommented:
Sorry for delay.

I thought you were using TemplateFields.

In this case your code is correct. The problem is elsewhere. I'd suspect that you bind old data to grid before Updating event takes place. A common error is data is bound to grid in Page_Load event ALWAYS but should be bound only on initial page load.

If you would give more code it might be helpful.

R
if (!this.IsPostBack)
{
  ... // bind grid here
}

Open in new window

0
 
Mr_ShawAuthor Commented:
thanks for the help all sorted now. The code which I used was

update_records_method.Record_ID = ((TextBox)user_records.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();

 
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.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now