Something that bothers me a lot when coding is best practice. I am completely self taught and I find it difficult to know whether I am doing things in the 'right way'.
Let's say I have a Gridview on a page which displays data from the following list:
(from foo in someContext.foobars select foo).ToList
Regardless or how simple or complex the query is, can I just bind the Gridview in the page code behind file as below (in Page.Load or wherever else I want it to run):
gvSomeGridview.DataSource = (from foo in someContext.foobars select foo).ToList(); gvSomeGridview.DataBind();
Or, should I have a class elsewhere that performs the query and returns a list and then bind the Gridview like:
gvSomeGridview.Datasource = helperClass.GetGridviewData gvSomeGridview.DataBind()
Perhaps go one further and also pass the GridView to the helper class as such:
If I am going to require this list elsewhere in my application or bind a different Gridview with the same data then clearly a class is the way forward, however if I do not, is it fine to just bind the GridView using the query in the code behind as per the first example, or is it recommended to keep all such operations in separate classes regardless?