目前我有几种方法接受GridView
对象作为参数,并且正在访问GridView
对象内的值以执行计算。C# - 传递GridView对象作为参数是一种好的做法吗?
我的问题是这是否是一种好的做法?我注意到,当我需要更改结果显示(即GridView
最初将有9个单元格而不是10个单元格的结果)时,我需要更改所有使用它的方法或冒险得到一个ArrayIndexOutOfBound
错误。
例
public static string checkCount(GridView gr)
{
//need to change the cell index if the sql to get the gridview is changed
string name = gr.Rows[b].Cells[9].Text.ToUpper();
}
对这个有什么想法?
感谢
编辑
是更好地有把SQL结果映射到对象的列表,该对象本身是数据行的表示方法。这种方式即使我删除了一列,我只需要处理映射方法本身,其他人应该按照标准运行。
实施例:
public class Profile{
string name {get;set;}
string icNo {get;set;}
}
public list getProfileList(){
//run some query here and loop the result
//while looping
profile = new Profile(name=/*the name result*/, icNo=/*icNo from result*/);
//return the list
}
网页加载
List profiles = getProfileList();
gv.DataSource = profiles;
gv.DataBind();
public string calculationMethod(List profiles){
//when in need of the result, just get the object from profiles and process
}
如果它可以让你避免重复代码,它是** **一个很好的做法。面临的挑战是建立合适的方法,在良好的层(留在UI层),避免使用frankenstein方法的复杂度较低。 –
重复的意义在于,无论何时我需要从gridview中获取数值名称,我必须使用涉及索引号的Text方法,并且当我更改sql时,我需要更改所有这些(想象把这条线分散在几个班上)。我认为这可能会导致维护问题,因此征求意见。 – ipohfly