Reputation: 883
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)?
Upvotes: 0
Views: 1084
Reputation: 460098
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
Upvotes: 1