[C#] DataGrid的使用 →→→→→进入此内容的聊天室

来自 , 2020-05-24, 写在 C#, 查看 150 次.
URL http://www.code666.cn/view/c3a690be
  1. private void DataGrid1_ItemCreated ( object sender, System.Web.UI.WebControls.DataGridItemEventArgs e )
  2. {
  3.           foreach ( DataGridItem di in this.DataGrid1.Items )
  4.         {
  5.                   if ( di.ItemType==ListItemType.Item||di.ItemType==ListItemType.AlternatingItem )
  6.                           
  7.                 {
  8.                            ( ( LinkButton ) di.Cells[8].Controls[0] ).Attributes.Add ( "onclick","return confirm(’确认删除此项吗?’);" );
  9.                           
  10.                 }
  11.         }
  12. }
  13.  
  14.  
  15. //样式交替:
  16. ListItemType itemType = e.Item.ItemType;
  17. if ( itemType == ListItemType.Item )
  18. {
  19.           e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor=’#FFFFFF’;";
  20.           e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor=’#d9ece1’;cursor=’hand’;" ;
  21. }
  22. else if ( itemType == ListItemType.AlternatingItem )
  23. {
  24.           e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor=’#a0d7c4’;";
  25.           e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor=’#d9ece1’;cursor=’hand’;" ;
  26. }
  27.  
  28.  
  29. //加一个编号列:
  30. DataTable dt= c.ExecuteRtnTableForAccess ( sqltxt ); //执行sql返回的DataTable
  31. DataColumn dc=dt.Columns.Add ( "number",System.Type.GetType ( "System.String" ) );
  32. for ( int i=0; i<dt.Rows.Count; i++ )
  33. {
  34.           dt.Rows["number"]= ( i+1 ).ToString();
  35. }
  36. DataGrid1.DataSource=dt;
  37. DataGrid1.DataBind();
  38.   DataGrid1中添加一个CheckBox,页面中添加一个全选框
  39. private void CheckBox2_CheckedChanged ( object sender, System.EventArgs e )
  40. {
  41.           foreach ( DataGridItem thisitem in DataGrid1.Items )
  42.           
  43.         {
  44.                    ( ( CheckBox ) thisitem.Cells[0].Controls[1] ).Checked=CheckBox2.Checked;
  45.                   
  46.         }
  47. }
  48.  
  49.  
  50. //将当前页面中DataGrid1显示的数据全部删除
  51. foreach ( DataGridItem thisitem in DataGrid1.Items )
  52. {
  53.           if ( ( ( CheckBox ) thisitem.Cells[0].Controls[1] ).Checked )
  54.                   
  55.         {
  56.                   string strloginid= DataGrid1.DataKeys[thisitem.ItemIndex].ToString();
  57.                   Del ( strloginid ); //删除函数
  58.                   
  59.         }
  60. }
  61.  
  62.  

回复 "DataGrid的使用"

这儿你可以回复上面这条便签

captcha