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

ASP.NET and C# Page View Counter: Using a Database

c# asp.net

I'm a working on a project similar to the concept of stackoverflow.com

How do I create this relationship in phpMyAdmin?

php mysql join foreign-keys phpmyadmin

I'm following along with a book that wants me to create the following relationships in my database:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.