2

I am trying to use the update method for the gridview and its simply returning null

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox Currency = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("Currency"));
        GridView1.EditIndex = -1;

        if (!Page.IsPostBack)
        {
            FillTravelers();
        }
    }

Any ideas? when I press update it just returns the field to its previous value but returns nothing.

Thanks

NaveenBhat
  • 3,248
  • 4
  • 35
  • 48
Karl
  • 781
  • 2
  • 9
  • 26
  • please post FillTravelers() method body. and what is returning null ? please specify it. – emre nevayeshirazi Aug 22 '11 at 12:37
  • fillTravelers is a method thats just binding the data, the problem is that the TextBox Currency = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("Currency")); Currency is returning null – Karl Aug 22 '11 at 12:42
  • ok. are you sure that your GridView has TextBox called 'Currency'? – emre nevayeshirazi Aug 22 '11 at 12:46
  • thats what i was thinking but i'm not sure how i could check that out, however the column name is currency for sure and when i go over the textbox with the mouse it says Currency, is there a better way i could check this? – Karl Aug 22 '11 at 12:48
  • You should be able to see the ID of TextBox from .aspx file. It should be something like, `` – emre nevayeshirazi Aug 22 '11 at 12:51
  • thats what i would do but this is an update, the values do not start as textbox, they become textboxes on the click of the edit button, when one clicks the edit button the values become editable, then change the values and click update – Karl Aug 22 '11 at 12:54

3 Answers3

1

I'm not sure you've shared enough detail, but my initial thought would be that if your "update" involves pressing a button (and thus, a postback) - your filltravelers won't be firing, because !Page.IsPostback will be false.

I'm assuming here that your method "FillTravelers" has scope to deal with filtering when you hit Update.

SpaceBison
  • 3,704
  • 1
  • 30
  • 44
  • but i dont think thats the problem, when i debug and i go through the reading of the field it returns null too...this is before fillTravelers – Karl Aug 22 '11 at 12:29
  • I think some more detail is required then as well as some additional actual code - NOT being able to see where the problem is makes it difficult to offer any further advice. – SpaceBison Aug 22 '11 at 12:33
  • protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { TextBox Currency = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("Currency")); could the problem be there? – Karl Aug 22 '11 at 12:39
  • It could be - does FindControl actually find the textbox, or does it return null. If it returns null, you need to tweak the way you interrogate the control for it's children as it did not find it. – SpaceBison Aug 22 '11 at 12:49
  • it seems to not be finding the textbox, i was thinking that the textbox id was wrong but im not sure since this is an update and not a normal textbox which is declated in aspx – Karl Aug 22 '11 at 12:55
  • Does this help? http://stackoverflow.com/questions/833490/asp-net-3-5-gridview-row-editing-dynamic-binding-to-a-dropdownlist – SpaceBison Aug 22 '11 at 13:10
1

As your Gridview is in its simplest form with no controls defined in the gridview (and hence there is no Currency textbox defined in the GridView), please check the datasource of GridView1 which I assume would be a Datatable. Then you need to identify which column of the Datatable is the Currency column.

For e.g., for the below datatable, it would be column 2.

  DataTable taskTable = new DataTable("AmountList");
  taskTable.Columns.Add("Id", typeof(int));
  taskTable.Columns.Add("Currency", typeof(string));
  taskTable.Columns.Add("Amount", typeof(decimal)); 

Based on this, you can retrieve your updated Currency value using the following code below.

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = TaskGridView.Rows[e.RowIndex];
        GridView1.EditIndex = -1;
        TextBox txtCurrency = (TextBox)(row.Cells[2].Controls[0]);
        if (!Page.IsPostBack)
        {
            FillTravelers();
        }
        //... Rest of the code
    }

Please note that if the Currency column is different above, then you need to change the index of rows.Cells[index].Controls[0] too. For e.g., if Currency is at column 5, then the txtCurrency definition becomes:

 TextBox txtCurrency = (TextBox)(row.Cells[5].Controls[0]);

If you are not able to figure out the column index of the datatable easily (or if you are using a more complicated datasource), then, assuming the GridView1 does not have too many columns, I would recommend trying to increment the Cells index step by step until you see the updated value in the Watch for row.Cells[index].Controls[0] while debugging.

Edit: (Adding code to retrieve the column index of the Currency column) You can pass "Currency" as a string and the OracleDataReader instance to the below method and this should give you the column index (or it gives you -1 if there is no Currency column in the OracleDataReader).

private int GetColumnIndexIfExists(OracleDataReader dr, string columnName)
{
    for (int i = 0; i < dr.FieldCount; i++)
    {
        if (dr.GetName(i).Equals(columnName))
        {
            return i;
        }
    }
    return -1;
}

Then in the actual code, you can modify add 1 to the cell index as shown below because the cell index is not zero based like the column index.

TextBox txtCurrency = (TextBox)(row.Cells[i+1].Controls[0]);
Kash
  • 8,799
  • 4
  • 29
  • 48
  • thanks for your reply, but im not using a datatable, instead i am using OracleDataReader and this problem still troubles me, not sure what more i can do :S – Karl Aug 23 '11 at 06:52
  • It should be relatively similar to a SQLDataReader where you can retrieve the columnindex of the Currency column. I will edit the answer to include the code. – Kash Aug 23 '11 at 14:42
  • You can still try the trial and error method of debugging with breakpoint in GridView1_RowUpdating and incrementing the cell number by 1 step by step (if you don't have too many columns) and observe value of each cell until you get the updated Currency value. I am assuming that the order of columns from the OracleDataReader would not change. – Kash Aug 23 '11 at 14:49
  • its ok, problem solved TextBox txtCurr = ((TextBox)(row.Cells[3].Controls[0])); – Karl Aug 24 '11 at 06:31
0

You get null as a result of FindControl() method since there is not any TextBox control named Currency.Try this,

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
   // Here you get the row that you want to edit.
   GridViewRow row = GridView1.Rows[e.RowIndex];
   // This one returns you the cell inside the row that is being edited.
   // Replace 1 with the column no you want.
   TableCell tableCell = row .Controls[1] as TableCell;
   // And finally you have your textbox.
   TextBox textBox = tableCell.Controls[0] as TextBox;        
}
emre nevayeshirazi
  • 18,983
  • 12
  • 64
  • 81