C# 如何在网格视图更新事件中从网格视图编辑事件获取保存在字符串中的值?
在gridview编辑事件中,我有:C# 如何在网格视图更新事件中从网格视图编辑事件获取保存在字符串中的值?,c#,asp.net,sql,C#,Asp.net,Sql,在gridview编辑事件中,我有: string name = GridView1.Rows[e.NewEditIndex].Cells[1].Text; string subname = GridView1.Rows[e.NewEditIndex].Cells[2].Text; 我想从行更新事件gridview中的变量中获取这些值,但我不知道如何访问它们 谢谢使用该属性 要获取更新行的旧值/新值,还可以使用和字典 protected void GridView1_RowUpdating(o
string name = GridView1.Rows[e.NewEditIndex].Cells[1].Text;
string subname = GridView1.Rows[e.NewEditIndex].Cells[2].Text;
我想从行更新事件gridview中的变量中获取这些值,但我不知道如何访问它们
谢谢使用该属性
要获取更新行的旧值/新值,还可以使用和字典
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=newdictionary();
foreach(e.NewValues中的DictionaryEntry条目)
{
如果(如:OldValues[entry.Key]!=entry.Value)
{
更改.添加(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);
changed=e.NewValues.Cast()
.Where(entry=>entry.Value!=e.OldValues[entry.Key])
.ToDictionary(entry=>entry.Key,entry=>entry.Value);
您可以使用以下代码:
受保护的无效页面加载(对象发送方、事件参数e)
{
//名称是屏幕上显示的字符串,名称是使用的变量。
//子名称是屏幕上显示的字符串,子名称是使用的变量
Response.Write("The Name"+ name );
Response.Write("The Subname" + subname);
}
谢谢,还有一个问题:如何从单元格中获取新值而不是旧值?我需要将新旧两种值添加到一个文本框中,就像更改的历史记录一样。ty@DanCristian:已编辑,使用调试器查看字典条目的实际命名方式。什么不起作用?我有VS 2010。是否收到错误?
changed = e.NewValues.Cast<DictionaryEntry>()
.Where(entry => entry.Value != e.OldValues[entry.Key])
.ToDictionary(entry => entry.Key, entry => entry.Value);
Response.Write("The Name"+ name );
Response.Write("The Subname" + subname);
}