Link to home
Start Free TrialLog in
Avatar of sharris_glascol
sharris_glascolFlag for United States of America

asked on

Only show certain Fields in a datagridview

How do I only display certain fields in sql query on a aspx c# page?  see code below using this in a load pdf page.

   
 
        }
        void LoadData()
        {
            var st = from s in db.Design_Tables select s;
            GridView1.DataSource = st;
            GridView1.DataBind();
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Instead of selecting s, have you tried selecting only the fields you want?

var st = from s in db.Design_Tables select new { s.Field1Name, s.Field2Name };

Open in new window

Avatar of sharris_glascol

ASKER

can a where clause be done here as well?
For Eric's proposed solution, don't forget to convert your result to a list; e.g. -
var st = (from s in db.Design_Tables select new { s.Field1Name, s.Field2Name }).ToList();

Open in new window

-saige-
Sure this is a LINQ query:

var st = from s in db.Design_Tables where s.Field1Name == "yourvalue" select new { s.Field1Name, s.Field2Name };

Open in new window

Yes you can add a where clause as well; e.g. -
var st = (from s in db.Design_Tables where s.FieldToCompare == "some value" select new { s.Field1Name, s.Field2Name }).ToList();

Open in new window

-saige-
that works but in the where clause how can I get it to == a textbook inside of the page?
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That worked!!  When I publish this to the web when I select my link button to the pdf folder it doesn't find it.  When I run locally on my pc if finds it, I think it's a path issue where still looking at local HD instead out on the server.

this is how the string is laid out

int rowindex = ((GridViewRow)((sender as Control)). NamingContainer).RowIndex;
string filelocation = Gridview1.Rows[rowindex].cells[2].text;
string FilePath = Server.MapPath("~/" + filelocation);

...any ideas??
I am not a web expert. It can be many things like missing privileges on the folder.

If the LINQ query is working, it is a brand new question then.