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 ?

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Nezir
  • 6,727
  • 12
  • 54
  • 78

1 Answers1

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.

Community
  • 1
  • 1
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
  • 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? – RB. Jan 10 '14 at 15:24
  • Yes RB, column names I am getting from db and those names will be different. – Nezir Jan 10 '14 at 15:36
  • @zire Ok, than just rename headers. – Ondrej Janacek Jan 10 '14 at 15:37
  • 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"; } } – Nezir Jan 10 '14 at 18:35