动态创建DataGrid 列排序功能的实现

动态创建的控件在回调的时候,将会被清空,所以事件更不能被执行,解决的方法是在LOAD事件里从新加载一次
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
    
/// <summary>
    
/// Summary description for WebForm1.
    
/// </summary>

    public class WebForm1 : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Button Button1;
        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// Put user code to initialize the page here

            
//            string f = @"\\10.1.22.77\test";
            
//
            
//            foreach (string s in System.IO.Directory.GetFiles(f))
            
//            {
            
//                Response.Write(s);
            
//                Response.Write("<br>");
            
//            }

            
if(Page.IsPostBack)
                CreateDataGrid();
        }


        
Web Form Designer generated code

        
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
        
{
        
        }


        
private void Button1_Click(object sender, System.EventArgs e)
        
{
            
            CreateDataGrid();

        }

        
protected string SortOrder 
        
{
            
get 
            
{
                
if(ViewState["DataGrid1SortOrder"== null
                
{
                    ViewState[
"DataGrid1SortOrder"= "ASC";
                }

                
return (string) ViewState["DataGrid1SortOrder"];
            }

            
set { ViewState["DataGrid1SortOrder"= value; }
        }

        
private void DataGrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
        
{
            DataTable dt
=(( (DataGrid)source).DataSource) as DataTable ;
            
string SortField;
            
if(SortOrder == "ASC"
            
{
                SortField 
= e.SortExpression + " DESC";
                SortOrder 
=  "DESC";
            }

            
else 
            
{
                SortField 
= e.SortExpression + " ASC";
                SortOrder 
=  "ASC";
            }

            dt.DefaultView.Sort
=SortField;
            DataGrid1.DataSource
=dt;
            DataGrid1.DataBind();
        }


        
private void CreateDataGrid()
        
{
            DataTable dt
=new DataTable ();
            dt.Columns.Add(
"ID");
            dt.Columns.Add(
"Name");
            
for(int i=0;i<10;i++)
            
{
                DataRow dr
=dt.NewRow();
                dr[
0]=i;
                dr[
1]="Name"+i;
                dt.Rows.Add(dr);
            }

                     
            BoundColumn dcID
=new BoundColumn();
            dcID.HeaderText
="ID";
            dcID.DataField
="ID";
            dcID.SortExpression
="ID";
            dcID.Visible
=true;

        
            
if(this.DataGrid1.Columns.Count<2)            
            
this.DataGrid1.Columns.Add(dcID);            

            DataGrid1.DataSource
=dt;
            DataGrid1.DataBind();

            DataGrid1.SortCommand
+=new DataGridSortCommandEventHandler(DataGrid1_SortCommand);}

    }

}

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>WebForm1</title>
        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        
<meta name="CODE_LANGUAGE" Content="C#">
        
<meta name="vs_defaultClientScript" content="JavaScript">
        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
            
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 232px; POSITION: absolute; TOP: 112px"
                runat
="server" AutoGenerateColumns="False" AllowSorting="True">
                
<Columns>
                    
<asp:BoundColumn DataField="Name" SortExpression="Name" HeaderText="Name "></asp:BoundColumn>
                
</Columns>
            
</asp:DataGrid>
            
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 496px; POSITION: absolute; TOP: 128px" runat="server"
                Text
="Button"></asp:Button>
        
</form>
    
</body>
</HTML>
posted @ 2007-07-06 17:15  瑞德船长  阅读(1107)  评论(1编辑  收藏  举报