2017-10-30 3 views
0

asp.net을 사용하여 웹 응용 프로그램에서 gridview를 구현하려고합니다. 나는 방법으로 내 DataGrid를 정렬 할 때 문제가 생기고 조언을 구하는 것이 좋습니다. 이건 내에서 .aspx 파일입니다ASP.NET Gridview 정렬 작동하지 않습니다.

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

:

<%@ 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> 

이는 .cs이

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(); 
    } 


} 
} 

파일입니다이 내 Web.config의 ConnectionString을이다

Gridview가 올바르게 채워지는데, "처리되지 않은 해고 이벤트 정렬"을 사용했습니다. 하지만 지금은 단순히 열을 정렬하려고하면 응답이 없습니다. 심지어 자동 생성 컬럼에서도 작동합니까? 정렬 식은 어디에 지정할 수 있습니까? (오름차순/내림차순 등)?

+0

이 튜토리얼을 살펴보십시오 : https://www.codeproject.com/Tips/663532/How-to-Perform-Sorting-in-Gri dview-in-ASP-NET 또는 https://stackoverflow.com/questions/23748174/gridview-sorting-tutorial – VDWWD

답변

1

당신은 !IsPostback하지 않을 경우 모든 연속 포스트 백에 초기 데이터 바인딩을 할 필요가 : GridView1_Sorting에서

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(); 
} 

당신이 데이터베이스에서 주문 데이터를 선택하고 그리드의 데이터 소스 속성에 할당해야 다음 전화 grvEmployee.DataBind() :

의 GridView 정렬 샘플 : https://stackoverflow.com/a/6602125/284240

+0

그래서 각 열에 대해 다른 정렬 방법을 만들어야합니까? 또한이 편집을 사용하면 열을 클릭 할 때 페이지가 완전히 비어있게됩니다. 죄송합니다, 정말 asp.net에서 새로운 문제. – onlyf

+0

@onlyf : 각 열에 왜 한 가지 방법이 있습니까? 메소드에'e.SortExpression'을 사용하는 코드가 이미 있습니다. 그러나, ASP.NET GridView로 정렬을 구현하는 방법을 보여주는 stackoverflow에 관한 많은 질문이 있습니다. 나는 한 시간 전에도 다음과 같이 제공했다 : https://stackoverflow.com/a/6602125/284240 –