From where I see this, the problem is with the hardcoded
[...Count - 1]
thing, why do that in the first place, and leave it exposed to several conditions? For example, what will happen if the
Count
property is zero (
the list is empty?). Instead of this, I would personally try using this,
GridViewRow row = null;
if(GridView1.Rows.Count == 0) {
} else if (GridView1.Rows.Count == 1) {
row = GridView1.Rows[0];
} else if (GridView1.Rows.Count > 1) {
row = GridView1.Rows[GridView1.Rows.Count - 1];
}
if(row != null) {
row.Attributes.Add("class", "highlighted");
}
This should properly ensure that your problems are targeted and there is a solution, that the code can take to prevent code crashes.