You can definitely bind var to gridview.
Let's assume there's an employee table with three fields
emp_id emp_fname emp_lname
First way: if you give the whole table or a particular record to var then to list would work...
employees is a data context class once you've established a successful connection to sql server..
var result = from alias in dc.employees
where alias.emp_id == id --(this is the passed parameter)
You can also manually specify the id, for example:
where alias.emp_id == 5
select alias;
The whole employee record having id = 5 would get selected.
You can now simply bind this to gridview
gridview1.datasource = result.tolist();
gridview1.databind();
If you give the whole table, then also tolist would work
var result = from alias in dc.employess
select alias;
gridview1.datasource = result.tolist();
gridview1.databind();
If you select multiple columns then tolist won't work. You need to return object.
the method has been defined in class1.
public static object returnquery()
{
dcdatacontext dc = new dcdatacontext();
var result = from alias in dc.employees
where alias.emp_id == 5
select new
{
alias.emp_fname,
alais.emp_lname
};
return result;
}
You need to catch the object.
object obj = new class1.returnquery();
gridview1.datasource = obj;
gridview1.databind();
Or you can simply try this and see if it works. I haven't tried this actually.
var result = from alias in dc.employees
where alias.emp_id == 5
select new
{
alias.emp_fname,
alias.emp_lname
};
gridview1.datasource= result.object();
gridview1.databind();