Karl
Karl

Reputation: 781

Gridview Update Returning null

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

Upvotes: 2

Views: 2135

Answers (3)

Kash
Kash

Reputation: 9019

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]);

Upvotes: 1

emre nevayeshirazi
emre nevayeshirazi

Reputation: 19241

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;        
}

Upvotes: 0

csharpnumpty
csharpnumpty

Reputation: 3723

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.

Upvotes: 1

Related Questions