2017-10-30 9 views
0

私はasp.netを使用してWebアプリケーションでgridviewを実装しようとしています。メソッドでデータグリッドをソートする際に問題が発生しました。アドバイスを求めるのが好きです。この私の.aspxファイルです:.csファイルがASP.NET Gridviewソートが機能しない

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


} 
} 

を提出さ

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

、これこれは私のweb.configたconnectionStringです:

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

グリッドビューが適切に設定されました。私は、処理されなかった「発生イベントソート」を取得するために使用されました。今は単に列をソートしようとすると応答がありません。これは自動生成された列でも機能しますか?ソート式はどこで指定できますか? (昇順/降順など)? 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(); 
} 

データベースからの注文データを選択し、グリッドのDataSourceプロパティに割り当て、その後、呼び出す必要があります:あなたは!IsPostbackは、すべての連続したポストバックでない場合にのみ、初期データバインディングを行う必要があり

+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

grvEmployee.DataBind()

GridViewのソートサンプル:https://stackoverflow.com/a/6602125/284240

+0

だから私は各列ごとに異なるソート方法を作成する必要がありますか?また、この編集で列をクリックすると、ページは単に完全に空白になります。問題を申し訳ありません、本当にasp.netで新しいです。 – onlyf

+0

@onlyf:なぜ各列に1つのメソッドがありますか? 'e.SortExpression'を使うメソッドをすでにコードしています。しかし、ASP.NET GridViewでソートを実装する方法を示すstackoverflowにはたくさんの疑問があります。私も以前から提供しています:https://stackoverflow.com/a/6602125/284240 –

関連する問題