在gridview编辑事件我有:
string name = GridView1.Rows[e.NewEditIndex].Cells[1].Text; string subname = GridView1.Rows[e.NewEditIndex].Cells[2].Text;
我想从行更新事件gridview中的变量中获取这些值,但我不知道如何访问它们.
谢谢
解决方法
使用
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; }
要获取更新行的旧值/新值,还可以使用GridViewUpdateEventArgs.OldValues
和GridViewUpdateEventArgs.NewValues
词典.
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"]; }
仅检测更改的值(未测试):
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); } }
或者使用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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。