目前我有幾種方法接受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