简体   繁体   中英

Access GridView Row Cell via Enum Extension a good idea?

Newbie here so not sure what are the potential performance issues here, and what are the best practices.

Is this a good idea? Thoughts?

FROM THIS: (prone to manual error when cells codes are all over the place and/or gridview changes)

row.Cells[1].Visible = false;   // CustomerId
row.Cells[9].Visible = false;   // OrderNumber
row.Cells[10].Visible = false;  // ProductId
. . .
int matchId = row.Cells[11].Text;
. . .

TO THIS: (more readable and manageable, just make sure the enum is in sync with the gridview)

row.Cells[Col.CustomerId.GetVal()].Visible = false;     
row.Cells[Col.OrderNumber.GetVal()].Visible = false; 
row.Cells[Col.ProductNumber.GetVal()].Visible = false;  
. . .
int matchId = row.Cells[Col.CustomerName.GetVal()].Text;
. . .


public enum Col
{
    MatchId = 1,
. . .
    ServiceCode = 9,
    CustomerId = 10,
    CustomerName = 11,
. . .

}

public static class ColExt
{
    static public int GetVal(this Col col)
    {
        return (int)col;
    }
}

Its good Idea if large amount of column is used in a grid or at many pages if same grid is being used. id will be easy to change column value from enum and it will reflect to all the grids....

In my opinion the code is more readable with the enums. But it's important that you have a code generator for the enum(s). Otherwise you do a lot of misstakes, if you have many columns to handle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM