Access to grid columns by name not index
Is it possible to access grid column by na开发者_开发技巧me not by index grid.columns["name"]
because if I add new column I must change all numbers?
Per row, you could use something like the following to get the index by name, and then use that to access the correct column.
int GetColumnIndexByName(GridViewRow row, string searchColumnName)
{
int columnIndex = 0;
foreach (DataControlFieldCell cell in row.Cells)
{
if (cell.ContainingField is BoundField)
{
if (((BoundField)cell.ContainingField).DataField.Equals(searchColumnName))
{
break;
}
}
columnIndex++;
}
return columnIndex;
}
Either way you need to create helper methods to go by name instead of the index.
精彩评论