c# – 如何从网格视图更新事件中的gridview编辑事件中获取保存在
发布时间:2020-12-15 17:24:25 所属栏目:百科 来源:网络整理
导读:在gridview编辑事件我有: string name = GridView1.Rows[e.NewEditIndex].Cells[1].Text;string subname = GridView1.Rows[e.NewEditIndex].Cells[2].Text; 我想从行更新事件gridview中的变量中获取这些值,但我不知道如何访问它们. 谢谢 解决方法 使用 RowI
在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; } 要获取更新行的旧值/新值,还可以使用 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |