0

i m trying to implement a gridview on a web application, using asp.net. I m encountering a problem when it comes to sorting my datagrid, by a method - and i d like to ask for advice. This is my .aspx file:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Employee.aspx.cs" Inherits="GridViewDemo1.Employee" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:GridView 
        ID="grvEmployee" 
        runat="server" 
        AutoGenerateColumns="true"
        BackColor="AliceBlue"
        ForeColor="Goldenrod" 
        BorderColor="YellowGreen"
        BorderStyle="Groove"
        Width="70%"
        CellPadding="3"
        CellSpacing="2" 
        AllowSorting="True"
        OnSorting="GridView1_Sorting" 
        AutoGenerateEditButton="true" 
        AutoGenerateDeleteButton="true" ViewStateMode="Enabled">  


        <RowStyle 
            HorizontalAlign="Center">
        </RowStyle>

        <FooterStyle
            ForeColor="#8C4510"
            BackColor="#F7DFB5">
        </FooterStyle>

        <PagerStyle 
            ForeColor="#8C4510" 
            HorizontalAlign="Center">
        </PagerStyle>

        <HeaderStyle 
            ForeColor="White" 
            Font-Bold="True" 
            BackColor="#A55129">
        </HeaderStyle>

    </asp:GridView>
</div>
</form>

and this is the .cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace GridViewDemo1
{
public partial class Employee : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        string selectSQL = "SELECT * from dbo.[User]";
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(selectSQL, con);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adapter.Fill(ds, "Employee");

        grvEmployee.DataSource = ds;
        grvEmployee.DataBind();

    }


    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        //dataTable.DefaultView.Sort = e.SortExpression;
        //grvEmployee.DataSource = dataTable;
        grvEmployee.DataBind();
    }


}
}

This is my web.config connectionstring:

  <connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=xxxxxx;Initial Catalog=yyyyyyy;User ID=zzzzz;Password=xxxxxx;" providerName="System.Data.SqlClient" />

The gridview gets populated properly, i used to get the "fired event Sorting which wasn't handled." but now i simply get no response from trying to sort a column. Does this even work with auto-generated columns? Where can i specify a sort expression? (Ascending/Descending etc)?

1

1 Answer 1

1

You have to do the initial DataBinding only if !IsPostback not on every consecutive postback:

if(!IsPostBack)
{
    string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    string selectSQL = "SELECT * from dbo.[User]";
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand(selectSQL, con);
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    adapter.Fill(ds, "Employee");

    grvEmployee.DataSource = ds;
    grvEmployee.DataBind();
}

In GridView1_Sorting you have to select the ordered data from the database and assign it to the grid's DataSource property, then call grvEmployee.DataBind():

GridView Sorting Sample:https://stackoverflow.com/a/6602125/284240

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

2 Comments

So i would need to create a different sorting method for each column? Also, with this edit when i click on a column, the page simply goes entirely blank. Sorry for the trouble, really new at asp.net.
@onlyf: why one method for each column? You have already code in your method that uses e.SortExpression. However, there are plenty of quesuions on stackoverflow which show how to implement sorting with an ASP.NET GridView. I have also provided one some time ago: stackoverflow.com/a/6602125/284240

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.