C# - Is passing on GridView object as parameter a good practice?

Currently I have few methods that accept GridView object as parameter and the value within the GridView object is being accessed to perform calculation.

My question is whether this is a good practice? I notice that when I need to change the result display (i.e. the GridView will have a result of 9 cells rather than 10 cells originally), I would need to change all the methods that uses it or risk getting an ArrayIndexOutOfBound error.

Example

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(); 
}

Any thoughts on this?

Thanks

EDIT Is it better to have a method that put map the sql result into a list of object, where the object itself is a representation of the data row. This way even if i remove a column, i just need to deal with the mapping method itself, others should be functioning as per norm.

Example:

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
}

Page Load

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
}

Answers


object as parameter and the value within the GridView object is being accessed to perform calculation

From where do the values come? I think a better practice would be to perform the calculations directly on your data/business object or a helper object which is responsible for only this specific calculation. Then you could have another function which dynamically creates the result view from this helper object instead of your BO or whatever.

will have a result of 9 cells rather than 10 cells originally

This is exactly why you should work not directly with the cells for calculation. And if you really need to do it, so you should look for another way to find the indices for the calculation. They could depend on the actual result object. (In which your values can be present also in the specific data type; only for visualization and user input the should converted to string or some-like)

In generell to solve this problem you should analyse clearly what are your needs. Keep some things like:

Also may think of using n-Tier model for your application


Need Your Help

Uncaught TypeError when using .replace() on HTML5 data attribute storing numbers retrieved with jQuery $.data()

javascript jquery html5 typeerror custom-data-attribute

I am storing database values in HTML5 data attributes via jQuery while first escaping them to be HTML attribute safe, and it works fine with Strings.

How to Calculate the Square Root of a Float in C#

c# math square-root

How can I calculate the square root of a Float in C#, similar to Core.Sqrt in XNA?