在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