遍匈 | 窒継囃兆 | 倖繁捲暦匂 | 匯送佚連酌陣盛舜狼由 | 倡亭麼字岑紛垂 | ASP 腎寂 | ASP室宝寄畠 | 和墮嶄伉 | 人薩捲暦嶄伉
  7i24 > ASP.Net室宝寄畠 > ASP.Net旗鷹赤坩 >
    7i24 .Com  
  匯倖糞嶐垓箔才、栽旺汽圷鯉、電會議DataGrid

7i24.Com音唯葎艇捲暦


恬宀済仇楳防 
參念壓asp載佃糞峇鷹嶷喘,asp.net載挫議盾畳阻宸倖諒籾,參和頁厘亟議DataGrid,写覚DataGrid,紗序阻幅週會/畠旺汽圷鯉/徭強箔才孔嬬,圻尖載酒汽,徽載挫議糞峙調鷹嶷喘.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
namespace SunService
{
 /// <summary>
 /// Summary description for DataGrid.
 /// </summary>
 [DefaultProperty("Text"),
 ToolboxData("<{0}:DataGrid runat=server></{0}:DataGrid>")]
 public class DataGrid : System.Web.UI.WebControls.DataGrid
 {
  private string text;
  private SqlDataAdapter adp;
  private DataSet ds;
  private DataView view;
  private string[] arritem;
  [Bindable(true),
  Category("Appearance"),
  DefaultValue("")]
  public string Text
  {
   get
   {
    return text;
   }

   set
   {
    text = value;
   }
  }
  /// <summary>
  /// protect SortDirection 電會圭
  /// </summary>

  public string SortDirection
  {
   get
   {
    if(ViewState["SortDirection"]==null)
    {
     return null;
    }
    else
    {
     if(ViewState["SortDirection"].ToString()=="")
     {
      return null;
     }
     else
     {
      return ViewState["SortDirection"].ToString();
     }
    }
   }
   set
   {
    ViewState["SortDirection"]=value;
   }
  }
  /// <summary>
  /// protect SortField 電會忖粁
  /// </summary>
  public string SortField
  {
   get
   {
    if(ViewState["SortField"]==null)
    {
     return null;
    }
    else
    {
     if(ViewState["SortField"].ToString()=="")
     {
      return null;
     }
     else
     {
      return ViewState["SortField"].ToString();
     }
    }
   }
   set
   {
    ViewState["SortField"]=value;
   }
  }
  /// <summary>
  /// sql 臥儂忖堪
  /// </summary>  
  public string selectCommandText
  {
   get
   {
    if(ViewState["selectCommandText"]==null)
    {
     return null;
    }
    else
    {
     if(ViewState["selectCommandText"].ToString()=="")
     {
      return null;
     }
     else
     {

      return ViewState["selectCommandText"].ToString();
     }
    }
   }
   set
   {
    ViewState["selectCommandText"]=value;
   }
  }
  /// <summary>
  /// 銭俊忖堪
  /// </summary>
  public string selectConnectionString
  {
   get
   {
    if(ViewState["selectConnectionString"]==null)
    {
     return null;
    }
    else
    {
     return ViewState["selectConnectionString"].ToString();
    }
   }
   set
   {
    ViewState["selectConnectionString"]=value;
   }
  }
  public DataTable Bindtable;
  public DataGrid()
  {
   this.Init+=new System.EventHandler(this.DataGrid_Init);
  }
  private void DataGrid_Init(object sender,EventArgs e)
  {

   this.Load+= new System.EventHandler(this.DataGrid_Load);   
   this.SortCommand+=new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid_SortCommand);
   this.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid_ItemDataBound);

  }
  private void DataGrid_Load(object sender,EventArgs e)
  {
   this.HorizontalAlign=HorizontalAlign.Center;
   this.AllowSorting=true;
   arritem=new string[256];
   ds=new DataSet();
   
   
  }
 

  /// <summary>
  /// GRID鰯協
  /// </summary>
  /// <param name="selectCommandText">臥儂忖堪</param>
  /// <param name="selectConnectionString">銭俊忖堪</param>
  public void BindGrid(string selectCommandText,string selectConnectionString)
  {
   this.selectCommandText=selectCommandText;
   this.selectConnectionString=selectConnectionString;
   BindGrid();
   
  }
  /// <summary>
  /// grid鰯協
  /// </summary>
  /// <param name="selectCommandText">臥儂忖堪</param>
  /// <param name="cn">銭俊斤</param>
  public void BindGrid(string selectCommandText,SqlConnection cn)
  {
   this.selectCommandText=selectCommandText;
   this.selectConnectionString=cn.ConnectionString;
   BindGrid();
  }
  /// <summary>
  /// grid鰯協,駅倬枠譜崔 selectCommmandText 式SelectConnectionString 奉來
  /// </summary>
  public void BindGrid()
  {
   if(this.selectCommandText!=null&&this.selectConnectionString!=null)
   {
    adp=new SqlDataAdapter(this.selectCommandText,this.selectConnectionString);
    adp.Fill(ds,"temp");
    view=ds.Tables["temp"].DefaultView;

    if(this.SortField!=null)
    {
     view.Sort=this.SortField+" "+this.SortDirection;
     int sortfieldindex=0;
     for( int i=0;i<ds.Tables["temp"].Columns.Count;i++)
     {
      if(ds.Tables["temp"].Columns[i].ColumnName==this.SortField)
      {
       sortfieldindex=i;
       break;
      }
     }
     string SortDirectionImg="§";
     if(this.SortDirection==" DESC")
     {
      SortDirectionImg="";

     }
     if(this.SortField!=this.DataKeyField)
     {
      ds.Tables["temp"].Columns[sortfieldindex].ColumnName+=SortDirectionImg;
     }
     
    }
    Bindtable=ds.Tables["temp"];
    DataRow row=Bindtable.NewRow();
    row[0]="悳柴:";    
    for(int i=1;i<Bindtable.Columns.Count;i++)
    { 
     Type t=Bindtable.Columns[i].DataType;
     if(t==typeof(Decimal)||t==typeof(Double)||t==typeof(Int16)||t==typeof(Int32)||t==typeof(Int64)||t==typeof(UInt16)||t==typeof(UInt32)||t==typeof(Int64))
     {
      row[i]=0;
      foreach( DataRow r in Bindtable.Rows)
      {
       try
       {
        row[i]=double.Parse(row[i].ToString())+double.Parse(r[i].ToString());
       }
       catch(Exception et)
       {
       
       }
       
      }
     }
    }
    Bindtable.Rows.Add(row);
    
    this.DataSource=view;
    this.DataBind();
    
   }
   else
   {
    
   }
  }
  private void DataGrid_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
  {
   
   if( this.SortDirection==" DESC")
   {
    this.SortDirection=" ASC";
   }
   else
   {
    this.SortDirection=" DESC";
   }
   
   this.SortField=e.SortExpression;
   this.SortField=this.SortField.Replace("§","");
   this.SortField=this.SortField.Replace("","");
   
   BindGrid();
  }


  private void DataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
   try
   {
    string txt="";
    for(int i=0;i<e.Item.Cells.Count;i++)
    {
     //     e.Item.Cells[i].Wrap=false;
     txt=e.Item.Cells[i].Text.Trim();
 
     if(myClass.IsDouble(txt))
     {
      e.Item.Cells[i].HorizontalAlign=HorizontalAlign.Right;
     }
     else
     {
      if(txt==arritem[i]&&txt!=""&&txt!=null)
      {
       e.Item.Cells[i].Text="";
      }
      else
      {
       arritem[i]=txt;
      }
     }
    }
   }
   catch(Exception et)
   {
    
   }

  }


 }
}


距喘酒汽:
委怏周溶欺匈中嶄 ,邪譜ID葎 DataGrid1:
距喘:DataGrid1.BindGrid(string selectCommandText,string selectConnectionString)
宸劔福阻秀 conntion DataAdapter DataSet壅鰯協議扮寂.
寄社珊辛委塋省閏簣塋掌駟/方忖塋掌駟週伴喀ItemDataBound並周嶄,珊嗤徭協吶蛍匈孔嬬吉.



  2002定1埖14晩  堋響 5788 肝  窟僕緩匈公涛嗔  栖坿    井幡尸咏  評茅

犢慘蔦贈   除豚犯泣

貧匯鐙 卞欺 ASP .NET歳菴実乃綴慴諒籾
和匯鐙 壓ASP.NET嶄強蓑伏撹夕侘
卦指貧匯蚊...
朴沫

(C)2004-2022 7i24.Com 隠藻侭嗤幡旋