Hi,
Please refer below Sql Query
SQL
DECLARE @Studentdetails AS TABLE(Class VARCHAR(10),Name VARCHAR(10),Roll INT,Year VARCHAR(20))
INSERT INTO @Studentdetails VALUES('M1','a','1','1st year')
INSERT INTO @Studentdetails VALUES('M2','b','1','1st year')
INSERT INTO @Studentdetails VALUES('M1','n','2','1st year')
INSERT INTO @Studentdetails VALUES('M1','f','3','1st year')
INSERT INTO @Studentdetails VALUES('M3','t','1','1st year')
INSERT INTO @Studentdetails VALUES('M2','w','2','1st year')
SELECT Class,[1st Year] as 'No of Students' FROM (
SELECT Class,Name,Year FROM @Studentdetails) as tbl
PIVOT (COUNT(Name) FOR [Year] IN ([1st year])) as pvt
also refer below link to bind data to gridview (i.e. Select Section)
GridView CRUD: Select Insert Edit Update Delete using Single Stored Procedure in ASP.Net
I hope this will help you out.