Ruan
Ruan

Reputation: 4283

How to Update ONLY the Gridview in C#

I have gridview in my C# ASP.net application.

When a user edits a row, all I want to do is update the gridview's datasource (Not the database yet, since I'm just taking the datagrid's datasource at the end and pushing it into my database as a dataset).

But The values I get are the old values, not the updated values im editing on the gridview.

Why is this happening? I'm following all the advice on these links.

    protected void grdViewDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)grdViewDetails.Rows[e.RowIndex];
        int adsf = row.Controls.Count;

        foreach (Control item in row.Controls)
        {
            if (item.Controls[0] is TextBox)
           {
               TextBox textbox = (TextBox)item.Controls[0];
               string x = textbox.Text;

//on the second for loop the string variable x = Employee1
           }
           if (item.Controls[0] is Label)
           {
               Label mylabel = (Label)item;
               //do stuff
           }

enter image description here

enter image description here

Now that textbox variable x should be xxxxxxx, but its still the old value.... why does this happen?

Cant you just update the gridview WITHOUT updating any other datasets/sources. Since I use the whole datasource of the gridview at the end.

Upvotes: 0

Views: 1087

Answers (1)

nmat
nmat

Reputation: 7591

You can access the new values with the GridViewUpdateEventArgs. Here's an example from the documentation:

  void CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
  {

    // Iterate through the NewValues collection and HTML encode all 
    // user-provided values before updating the data source.
    foreach (DictionaryEntry entry in e.NewValues)
    {

      e.NewValues[entry.Key] = Server.HtmlEncode(entry.Value.ToString());

    }
  }

You can use the debugger to inspect your e variable more closely and find out which values do you need.

Upvotes: 3

Related Questions