4

On gridview editing event I have :

string name = GridView1.Rows[e.NewEditIndex].Cells[1].Text;
string subname = GridView1.Rows[e.NewEditIndex].Cells[2].Text;

I want to get those values from variables in row updating event gridview but I don't know how to access them.

thanks

2 Answers 2

3

Use the RowIndex property

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{    
    GridViewRow row = GridView1.Rows[e.RowIndex];
    string name     = row.Cells[1].Text;
    string subname  = row.Cells[2].Text;
}

To get the old/new values of an updating row, you can also use the GridViewUpdateEventArgs.OldValues and GridViewUpdateEventArgs.NewValues Dictionaries.

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{    
    string oldName     = e.OldValues["Name"]; 
    string oldSubname  = e.OldValues["SubName"];
    string newName     = e.NewValues["Name"];
    string newSubname  = e.NewValues["SubName"];
}

To detect only the changed values(not tested):

var changed = new Dictionary<Object, Object>();
foreach (DictionaryEntry entry in e.NewValues)
{
    if (e.OldValues[entry.Key] != entry.Value)
    {
        changed.Add(entry.Key, entry.Value);
    }
}

or with LINQ:

changed = e.NewValues.Cast<DictionaryEntry>()
          .Where(entry => entry.Value != e.OldValues[entry.Key])
          .ToDictionary(entry => entry.Key, entry => entry.Value);

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdating.aspx

Sign up to request clarification or add additional context in comments.

3 Comments

thanks , another question : how to get the new value from cells instead of the old value? I need both old+new to add in a textbox like a history of what was changed. ty
@DanCristian: Edited, use the debugger to see how the dictionary entries are actually named.
What doesnt work? I have VS 2010. Do you get an error?
-1

You can use the code:

protected void Page_Load(object sender, EventArgs e) { //The Name is string display on screen and name is variable used . //The Subame is string display on screen and subname is variable used .

    Response.Write("The Name"+ name );
    Response.Write("The Subname" + subname);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.