2

I have a LINQ query and as you can see I am trying to use variables as column names.

string first = "someColumnName1";
string sec = "someColumnName2";
string third = "someColumnName3";

var data = (from i in ctx.PlanskaKalkulacijas
            where i.PKid.Value == id
            select new { first = i.NP1, sec = i.NP2, third = i.NP3}
           ).OrderByDescending(m => m.Id)
            .ToList();

this code produce column names in gridview as first, sec and third but I need someColumnName1, someColumnName2, someColumnName3.

Any help ?

1 Answer 1

3

Either rename columns in the gridview after you insert data or name properties of the anonymous type you create properly.

var data = (from i in ctx.PlanskaKalkulacijas 
            where i.PKid.Value == id 
            select new 
            { 
                someColumnName1 = i.NP1, 
                someColumnName2 = i.NP2, 
                someColumnName3 = i.NP3
            }).OrderByDescending(m => m.Id)
              .ToList();

If you don't know values at compile time, just rename it. To do that, follow this question here on SO.

Sign up to request clarification or add additional context in comments.

4 Comments

I suspect that the question author does not actually know the column names at compile time. @zire - can you clarify if you know the column names at compile time?
Yes RB, column names I am getting from db and those names will be different.
@zire Ok, than just rename headers.
Thanks again Ondrej this works:protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { e.Row.Cells[0].Text = "Date"; } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.