We help IT Professionals succeed at work.

Only show certain Fields in a datagridview

sharris_glascol
sharris_glascol asked
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();
Comment
Watch Question

Éric MoreauSenior .Net Consultant
Top Expert 2016
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

sharris_glascolIT Manager

Author

Commented:
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-
Éric MoreauSenior .Net Consultant
Top Expert 2016
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-
sharris_glascolIT Manager

Author

Commented:
that works but in the where clause how can I get it to == a textbook inside of the page?
Senior .Net Consultant
Top Expert 2016
where s.FieldToCompar == YourControlName.PropertyContainingTheValue

Open in new window

sharris_glascolIT Manager

Author

Commented:
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??
Éric MoreauSenior .Net Consultant
Top Expert 2016
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.