直播中
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">連接對(duì)象</param>
public void BindGrid(string selectCommandText,SqlConnection cn)
{
this.selectCommandText=selectCommandText;
this.selectConnectionString=cn.ConnectionString;
BindGrid();
}
/// <summary>
/// grid綁定,必須先設(shè)置 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]="總計(jì):";
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)
{
}
}
}
}
調(diào)用簡(jiǎn)單:
把組件拖到頁(yè)面中 ,假設(shè)ID為 DataGrid1:
調(diào)用:DataGrid1.BindGrid(string selectCommandText,string selectConnectionString)
這樣省了建 conntion DataAdapter DataSet再綁定的時(shí)間.
大家還可把顯示時(shí)間顯示格式/數(shù)字顯示格式等加進(jìn)ItemDataBound事件中,還有自定義分頁(yè)功能等.