0

I am using ASP.NET 4.0. I checked a couple of links on this forum as well, but didn't find a solution that fits my requirement.

I have a GridView which gets populated using a query in a method. The query is below:

SELECT district_name, count(record_id) FROM districts;

I have disabled Auto Create Columns options and manually added two columns, viz: District and Count. It is obvious that the second column, i.e., Count is not in the DataSource as it is a computed result.

Someone suggested me to use #Eval("count") and so I tried:

<asp:BoundField DataField=<%#Eval("count") %> /> 

but it is also not working. I tried creating TemplateField, but not failed to make it work.

How to include this count column in the GridView?

Pankaj
  • 9,749
  • 32
  • 139
  • 283
RKh
  • 13,818
  • 46
  • 152
  • 265
  • give alias to computed column count(record_id) like "SELECT district_name, count(record_id) as Count1 FROM districts;" and use it like Eval("count1") – rt2800 Apr 17 '12 at 11:15
  • Error: Literal content (' – RKh Apr 17 '12 at 11:20

2 Answers2

0

Replace

SELECT district_name, count(record_id) FROM districts;

<asp:BoundField DataField=<% #Eval("count") %> />

With

SELECT district_name, count(record_id) as count FROM districts;

<asp:BoundField DataField="count" />
Pankaj
  • 9,749
  • 32
  • 139
  • 283
  • I used like this: /> but it is giving error: Literal content (' – RKh Apr 17 '12 at 11:32
  • Previous one is also working. I am using ORM and a POCO was conflicting. Added a custom column with the name "Count" in the POCO, fixed the issue. – RKh Apr 17 '12 at 12:18
0

I had commas between the BoundField attributes like an idiot...

JoshNaro
  • 2,047
  • 2
  • 20
  • 40