I have written a query in SQL server 2008 R2 and it is:

select *, Row_Number() Over(Order By Total_Marks DESC) as Rank
from tbl_Ranking_System 
order by Rank

In this query, I have taken variable called "Rank" and I am getting the value as expected. Now I am trying to map this Rank to a Column I have taken in the gridview. I am not able to see any value in the Rank column in the gridview. But when i execute in back end i am able to see the result. Please help me in mapping this Rank to the gridview column Rank.


I assume you have a GridView with at least single column Rank as shown below

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White" CellPadding="5">
            <asp:BoundField DataField="Rank" HeaderText="Rank" />

On the page load, you could prepare a query, fetch the results and bind them with the gridview.

protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)
        DataTable dt;
        String SQL = "SELECT *, Row_Number() Over(Order By Total_Marks DESC) AS Rank FROM tbl_Ranking_System ORDER BY Rank";

        string sConstr = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(sConstr))
            using (SqlCommand comm = new SqlCommand(SQL, conn))
                using (SqlDataAdapter da = new SqlDataAdapter(comm))
                    dt = new DataTable("tbl");

        GridView1.DataSource = dt;
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.