反射

/// <summary>
/// 更新数据时给GridView赋值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="gridview">表格</param>
/// <param name="rowHandle">gridview中数据的行索引</param>
/// <param name="fieldName">唯一值字段名称</param>
/// <param name="value">实体</param>
public static void SetRowValue<T>(this GridView gridview, int rowHandle, string fieldName, T value)
{
var list = gridview.DataSource as List<T>;
var compId = value.GetType().GetProperty(fieldName).GetValue(value, null);
int index = list.FindIndex(_ =>
_.GetType().GetProperty(fieldName).GetValue(_, null).ToString() == compId.ToString());
list[index] = value;
gridview.RefreshRow(rowHandle);
}

posted @ 2024-01-23 15:50  越吃越瘦  阅读(6)  评论(0编辑  收藏  举报