在我查看旧代码的过程中,我遇到了这个问题:
private void InitializeRow(GridViewRowInfo destRow, GridViewRowInfo sourceRow)
{
destRow.Cells["Image"].Value = sourceRow.Cells["Image"].Value;
destRow.Cells["Name"].Value = sourceRow.Cells["Name"].Value;
destRow.Cells["Country"].Value = sourceRow.Cells["Country"].Value;
destRow.Cells["Price"].Value = sourceRow.Cells["Price"].Value;
}
我想用更通用的东西重写它,不必将每个列的名称都写成字符串。假设它们是相同但不是相同的顺序,所以我不能使用索引。进入这样的事情:
private void InitializeRow(GridViewRowInfo destRow, GridViewRowInfo sourceRow)
{
foreach (GridViewCellInfoCollection Csource in sourceRow.Cells)
{
destRow.Cells.OfType<Csource.GetType()> =
sourceRow.Cells.OfType<Csource.GetType()> ;
}
}
答案 0 :(得分:1)
假设这是针对Telerik网格的,您可以执行以下操作:
private void InitializeRow(GridViewRowInfo destRow, GridViewRowInfo sourceRow)
{
foreach (GridViewCellInfo Csource in sourceRow.Cells)
{
destRow.Cells[Csource.ColumnInfo.Name].Value = Csource.Value;
}
}