How to get values ​​stored in rows from gridview edit event in grid update event?

In the gridview edit 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 these values ​​from variables in gridview grid update, but I don't know how to get them.

thanks

+4
source share
2 answers

Use the RowIndex

 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 old / new update row values, you can also use 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 changed values (not verified) :

 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

+2
source

You can use the code:

protected void Page_Load (object sender, EventArgs e) {// The name is a string display on the screen, and the name is a variable. // Subame is a string display on the screen, and the subrecord is a variable.

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

Source: https://habr.com/ru/post/1399982/


All Articles