Pierre Lebon Pierre Lebon - 23 days ago 5
C# Question

Generic version of this function

In my quest of reviewing my old code i came across this:

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;
}


I want to rewrite it in something more generic, to not have to write every columns name as string. Assuming that they are the same but not in the same order so i can't use index. Into something like this :

private void InitializeRow(GridViewRowInfo destRow, GridViewRowInfo sourceRow)
{
foreach (GridViewCellInfoCollection Csource in sourceRow.Cells)
{
destRow.Cells.OfType<Csource.GetType()> = sourceRow.Cells.OfType<Csource.GetType()> ;
}
}

Answer

Assuming this is for Telerik grid, you can probably do something like this:

private void InitializeRow(GridViewRowInfo destRow, GridViewRowInfo sourceRow) 
{
    foreach (GridViewCellInfo Csource in sourceRow.Cells)
    { 
        destRow.Cells[Csource.ColumnInfo.Name].Value = Csource.Value
     }
}