DataGrid使用

本文介绍了ASP.NET中DataGrid控件的多种实用操作方法,包括如何为DataGrid中的删除按钮添加确认提示、实现行的样式交替效果、自动添加编号列、在DataGrid中加入CheckBox并实现全选功能,以及如何通过CheckBox选择并删除DataGrid中的记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  添加删除确认: 

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

 foreach(DataGridItem di in this.DataGrid1.Items) 
 { 
  if(di.ItemType==ListItemType.Item||di.ItemType==ListItemType.AlternatingItem) 
  { 
   ((LinkButton)di.Cells[8].Controls[0]).Attributes.Add("onclick","return confirm(’确认删除此项吗?’);"); 
  } 
 } 
}  

  样式交替: 

ListItemType itemType = e.Item.ItemType; 

if (itemType == ListItemType.Item ) 

 e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor=’#FFFFFF’;"; 
 e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor=’#d9ece1’;cursor=’hand’;" ; 

else if( itemType == ListItemType.AlternatingItem) 

 e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor=’#a0d7c4’;"; 
 e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor=’#d9ece1’;cursor=’hand’;" ; 
}  

  添加一个编号列: 

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

  DataGrid1中添加一个CheckBox,页面中添加一个全选框 

private void CheckBox2_CheckedChanged(object sender, System.EventArgs e) 

 foreach(DataGridItem thisitem in DataGrid1.Items) 
 { 
  ((CheckBox)thisitem.Cells[0].Controls[1]).Checked=CheckBox2.Checked; 
 } 
}  

  将当前页面中DataGrid1显示的数据全部删除 

foreach(DataGridItem thisitem in DataGrid1.Items) 

 if(((CheckBox)thisitem.Cells[0].Controls[1]).Checked) 
 { 
  string strloginid= DataGrid1.DataKeys[thisitem.ItemIndex].ToString(); 
  Del (strloginid); //删除函数 
 } 
}  
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值