对DataGrid的基础操作总结

1 .删除表格选定记录
int  intEmpID  =  ( int )MyDataGrid.DataKeys[e.Item.ItemIndex]; 
string  deleteCmd  =   " DELETE from Employee where emp_id =  "   +  intEmpID.ToString() ;
2 .删除表格记录警告
private   void  DataGrid_ItemCreated(Object sender,DataGridItemEventArgs e)  // 在创建项时激发

 
switch(e.Item.ItemType) 
 

  
case ListItemType.Item : 
  
case ListItemType.AlternatingItem : 
  
case ListItemType.EditItem: 
   TableCell myTableCell; 
   myTableCell 
= e.Item.Cells[14]; 
   LinkButton myDeleteButton ; 
   myDeleteButton 
= (LinkButton)myTableCell.Controls[0]; 
   myDeleteButton.Attributes.Add(
"onclick","return confirm(’您是否确定要删除这条信息’);"); 
   
break
  
default
   
break
 }
 
}
 
3 .点击表格行链接另一页

private   void  grdCustomer_ItemDataBound( object  sender, System.Web.UI.WebControls.DataGridItemEventArgs e) 

     
//点击表格打开 
    if (e.Item.ItemType == ListItemType.Item e.Item.ItemType == ListItemType.AlternatingItem) 
  e.Item.Attributes.Add(
"onclick","window.open(’Default.ASPx?id=" + e.Item.Cells[0].Text + "’);"); 
}
 
  
// 双击表格连接到另一页 

  在itemDataBind事件中 

if (e.Item.ItemType  ==  ListItemType.Item e.Item.ItemType  ==  ListItemType.AlternatingItem) 

 
string OrderItemID =e.item.cells[1].Text; 
 ... 
 e.item.Attributes.Add(
"ondblclick""location.href=’../ShippedGrid.ASPx?id=" + OrderItemID + ""); 
}
 
  
// 双击表格打开新一页 

if (e.Item.ItemType  ==  ListItemType.Item e.Item.ItemType  ==  ListItemType.AlternatingItem) 

 
string OrderItemID =e.item.cells[1].Text; 
 ... 
 e.item.Attributes.Add(
"ondblclick""open(’../ShippedGrid.ASPx?id=" + OrderItemID + "’)"); 
}
 
4 .表格超连接列传递参数

<ASP:HyperLinkColumn Target
= " _blank "  headertext = " ID号 "  DataTextField = " id "  NavigateUrl = " aaa.ASPx?id=’<%# DataBinder.Eval(Container.DataItem,  " 数据字段1 " )%>’ & name=’<%# DataBinder.Eval(Container.DataItem,  " 数据字段2 " )%>’ /> 
5 .表格点击改变颜色

if  (e.Item.ItemType  ==  ListItemType.Item e.Item.ItemType  ==  ListItemType.AlternatingItem) 

     e.Item.Attributes.Add(
"onclick","this.style.backgroundColor=’#99cc00’; 
  this.style.color=’buttontext’;this.style.cursor=default’;"); 
}
 
  
// 写在DataGrid的_ItemDataBound里 

if  (e.Item.ItemType  ==  ListItemType.Item e.Item.ItemType  ==  ListItemType.AlternatingItem) 

            e.Item.Attributes.Add(
"onmouseover","this.style.backgroundColor=’#99cc00’; 
   this.style.color=’buttontext’;this.style.cursor=default’;"); 
            e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=’’;this.style.color=’’;"); 
}
 
6 .关于日期格式

日期格式设定 

DataFormatString
= " {0:yyyy-MM-dd} "  
我觉得应该在itembound事件中 

e.items.cell[
" 你的列 " ].text = DateTime.Parse(e.items.cell[ " 你的列 " ].text.ToString( " yyyy-MM-dd " )) ;
7 .获取错误信息并到指定页面

不要使用Response.Redirect,而应该使用Server.Transfer 

e.g :

//  in global.asax 
protected   void  Application_Error(Object sender, EventArgs e) 
if (Server.GetLastError() is HttpUnhandledException) 
Server.Transfer(
"MyErrorPage.ASPx"); 

//其余的非HttpUnhandledException异常交给ASP.net自己处理就okay了 :) 
}
 
Redirect会导致post-back的产生从而丢失了错误信息,所以页面导向应该直接在服务器端执行,这样就可以在错误处理页面得到出错信息并进行相应的处理 
8 .实现全选
protected   void  CheckAll_CheckedChanged( object  sender, System.EventArgs e) 

 
//改变列的选定,实现全选或全不选。 
 CheckBox chkEXPort ; 
 
if( CheckAll.Checked) 
 

  
foreach(DataGridItem oDataGridItem in MyDataGrid.Items) 
  

   chkEXPort 
= (CheckBox)oDataGridItem.FindControl("chkEXPort"); 
   chkEXPort.Checked 
= true
  }
 
 }
 
 
else 
 

  
foreach(DataGridItem oDataGridItem in MyDataGrid.Items) 
  

   chkEXPort 
= (CheckBox)oDataGridItem.FindControl("chkEXPort"); 
   chkEXPort.Checked 
= false
  }
 
 }
 
}
 
9 .数字格式化

【<
% #Container.DataItem( " price " ) % >的结果是500. 0000 ,怎样格式化为500. 00 ? 】 

% #Container.DataItem( " price " , " {0:¥#,##0.00} " ) % > 

int  i = 123456
string  s = i.ToString( " ###,###.00 " ); 
10 .日期格式化

【ASPx页面内:<
% # DataBinder.Eval(Container.DataItem, " Company_Ureg_Date " ) % > 显示为:  2004 - 8 - 11   19 : 44 : 28  我只想要: 2004 - 8 - 11  】 

% # DataBinder.Eval(Container.DataItem, " Company_Ureg_Date " , " {0:yyyy-M-d} " ) % > 
应该如何改? 

【格式化日期】 

取出来,一般是object((DateTime)objectFromDB).ToString(
" yyyy-MM-dd " ); 

【日期的验证表达式】 

A.以下正确的输入格式: [
2004 - 2 - 29 ], [ 2004 - 02 - 29   10 : 29 : 39  pm], [ 2004 / 12 / 31

^ ((d {2} (([ 02468 ][ 048 ])([ 13579 ][ 26 ]))[ - / s] ? (((( 0 ? [ 13578 ])( 1 [ 02 ]))[ - / s] ? (( 0 ? [ 1 - 9 ])([ 1 - 2 ][ 0 - 9 ])( 3 [ 01 ])))((( 0 ?

[
469 ])( 11 ))[ - / s] ? (( 0 ? [ 1 - 9 ])([ 1 - 2 ][ 0 - 9 ])( 30 )))( 0 ? 2 [ - / s] ? (( 0 ? [ 1 - 9 ])([ 1 - 2 ][ 0 - 9 ])))))(d {2} (([ 02468 ][ 1235679 ])

([
13579 ][ 01345789 ]))[ - / s] ? (((( 0 ? [ 13578 ])( 1 [ 02 ]))[ - / s] ? (( 0 ? [ 1 - 9 ])([ 1 - 2 ][ 0 - 9 ])( 3 [ 01 ])))((( 0 ? [ 469 ])( 11 ))[ - /

s]
? (( 0 ? [ 1 - 9 ])([ 1 - 2 ][ 0 - 9 ])( 30 )))( 0 ? 2 [ - / s] ? (( 0 ? [ 1 - 9 ])( 1 [ 0 - 9 ])( 2 [ 0 - 8 ]))))))(s((( 0 ? [ 1 - 9 ])( 1 [ 0 - 2 ])):([ 0 - 5 ][ 0 - 9 ])

((s)(:([
0 - 5 ][ 0 - 9 ])s))([AMPMampm] {2,2} ))) ?
B.以下正确的输入格式:[
0001 - 12 - 31 ], [ 9999   09   30 ], [ 2002 / 03 / 03

^ d {4} [ - / s] ? (((( 0 [ 13578 ])( 1 [ 02 ]))[ - / s] ? (([ 0 - 2 ][ 0 - 9 ])( 3 [ 01 ])))((( 0 [ 469 ])( 11 ))[ - / s] ? (([ 0 - 2 ][ 0 - 9 ])( 30 )))( 02 [

- / s] ? [ 0 - 2 ][ 0 - 9 ]))$ 
【大小写转换】 

HttpUtility.HtmlEncode(
string ); 
HttpUtility.HtmlDecode(
string
11 .读取DataGrid控件TextBox值
foreach (DataGrid dgi  in  yourDataGrid.Items) 

 TextBox tb 
= (TextBox)dgi.FindControl("yourTextBoxId"); 
 tb.Text.... 
}
 
12 .在DataGrid中有3个模板列包含Textbox分别为 DG_ShuLiang (数量) DG_DanJian(单价) DG_JinE(金额)分别在5. 6 .7列,要求

在录入数量及单价的时候自动算出金额即:数量
* 单价 = 金额还要求录入时限制为数值型.我如何用客户端脚本实现这个功能 ?  


〖思归〗 

<ASP:TemplateColumn HeaderText
= " 数量 " > 
<ItemTemplate> 
<ASP:TextBox id
= " ShuLiang "  runat = ’server’ Text = ’< % # DataBinder.Eval(Container.DataItem, " DG_ShuLiang " ) % >’ 
onkeyup
= " Javascript:DoCal() "  
/ > 

<ASP:RegularEXPressionValidator id
= " revS "  runat = " server "  ControlToValidate = " ShuLiang "  ErrorMessage = " must be 

integer
"  ValidationEXPression= " ^ d + $ "  /> 
/ ItemTemplate> 
/ ASP:TemplateColumn> 

<ASP:TemplateColumn HeaderText
= " 单价 " > 
<ItemTemplate> 
<ASP:TextBox id
= " DanJian "  runat = ’server’ Text = ’< % # DataBinder.Eval(Container.DataItem, " DG_DanJian " ) % >’ 
onkeyup
= " Javascript:DoCal() "  
/ > 

<ASP:RegularEXPressionValidator id
= " revS2 "  runat = " server "  ControlToValidate = " DanJian "  ErrorMessage = " must be 

numeric
"  ValidationEXPression= " ^ d + (.d * ) ? $ "  /> 

/ ItemTemplate> 
/ ASP:TemplateColumn> 

<ASP:TemplateColumn HeaderText
= " 金额 " > 
<ItemTemplate> 
<ASP:TextBox id
= " JinE "  runat = ’server’ Text = ’< % # DataBinder.Eval(Container.DataItem, " DG_JinE " ) % >’  / > 
/ ItemTemplate> 
/ ASP:TemplateColumn><script language = " Javascript " > 
function DoCal() 

 var e 
= event.srcElement; 
 var row 
= e.parentNode.parentNode; 
 var txts 
= row.all.tags("INPUT"); 
 
if (!txts.length txts.length < 3
  
return

 var q 
= txts[txts.length-3].value; 
var p 
= txts[txts.length-2].value; 

 
if (isNaN(q) isNaN(p)) 
  
return

 q 
= parseInt(q); 
 p 
= parseFloat(p); 

 txts[txts.length
-1].value = (q * p).toFixed(2); 
}
 
/ script> 
13 .datagrid选定比较底下的行时,为什么总是刷新一下,然后就滚动到了最上面,刚才选定的行因屏幕的关系就看不到了。
page_load 
page.smartNavigation
= true
14 .在Datagrid中修改数据,当点击编辑键时,数据出现在文本框中,怎么控制文本框的大小  ?  
private   void  DataGrid1_ItemDataBound(obj sender,DataGridItemEventArgs e) 

 
for(int i=0;i<e.Item.Cells.Count-1;i++
  
if(e.Item.ItemType==ListItemType.EditType) 
  

   e.Item.Cells[i].Attributes.Add(
"Width""80px"
  }
 
}
 
15 .自定义分页代码: 

先定义变量 : 

public   static   int  pageCount;  // 总页面数 
public   static   int  curPageIndex = 1 // 当前页面 
// 下一页: 

if (DataGrid1.CurrentPageIndex < (DataGrid1.PageCount  -   1 )) 

 DataGrid1.CurrentPageIndex 
+= 1
 curPageIndex
+=1
}
 

bind(); 
//  DataGrid1数据绑定函数 
// 上一页: 

if (DataGrid1.CurrentPageIndex > 0

 DataGrid1.CurrentPageIndex 
+= 1
 curPageIndex
-=1
}
 

bind(); 
//  DataGrid1数据绑定函数 
// 直接页面跳转: 

int  a = int .Parse(JumpPage.Value.Trim()); // JumpPage.Value.Trim()为跳转值 

if (a<DataGrid1.PageCount) 

 
this.DataGrid1.CurrentPageIndex=a; 
}
 

bind(); 
16 .添加一个编号列:

DataTable dt
=  c.ExecuteRtnTableForAccess(sqltxt);  // 执行sql返回的DataTable 
DataColumn dc = dt.Columns.Add( " number " ,System.Type.GetType( " System.String " )); 

for ( int  i = 0 ;i<dt.Rows.Count;i ++

 dt.Rows[i][
"number"]=(i+1).ToString(); 
}
 

DataGrid1.DataSource
= dt; 
DataGrid1.DataBind(); 
17 .将当前页面中DataGrid1显示的数据全部删除
 
foreach (DataGridItem thisitem  in  DataGrid1.Items) 

 
if(((CheckBox)thisitem.Cells[0].Controls[1]).Checked) 
 

  
string strloginid= DataGrid1.DataKeys[thisitem.ItemIndex].ToString(); 
  Del (strloginid); 
//删除函数 
 }
 
}
 
注:本文转自网络。

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值