ASP.NET GridView的使用详解

 

这是要实现的功能:

 

第一步:拖入GridView控件 并且完成查询所有数据的方法 通过this.GridView1.DataSource 获取集合数据   GridView1.DataBind() 绑定数据

 

第二步:实现全选功能

1. 页面代码:

 

ExpandedBlockStart.gif 代码
  < asp:TemplateField  HeaderText ="全选" >
                
< HeaderTemplate >
                    
< input  type ="checkbox"  id ="CheckBox1"  name ="CheckBox1"  onclick ="GetAllCheckBox(this)"   />
                    全选
                
</ HeaderTemplate >
                
< ItemTemplate >
                    
< input  type ="checkbox"  name ="CheckBox2"   />
                
</ ItemTemplate >
                
< ItemStyle  HorizontalAlign ="Center"  Width ="100px"   />
</ asp:TemplateField >

 

 2. 实现全选的JS代码:

ExpandedBlockStart.gif 代码
     < script type = " text/javascript "  language = " javascript " >
        
function  GetAllCheckBox(checkAll){
            
var  items  =  document.getElementsByName( " CheckBox2 " );
            
for ( var  i = 0 ;i < items.length;i ++ ){
                items[i].checked 
=  checkAll.checked;
            }
        }
    
< / script>

 

第三步:实现光棒效果 突出显示被选中的行

编写RowDataBound事件 代码

 

ExpandedBlockStart.gif 代码
     protected   void  GridView1_RowDataBound( object  sender, GridViewRowEventArgs e)
    {
        
/* *
         * 实现光棒效果
         * 
*/
        
if  (e.Row.RowType  ==  DataControlRowType.DataRow) // 判断是否是数据行 去除第一行(标题行)
        {
            e.Row.Attributes.Add(
" onmouseover " " currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff' " );
            e.Row.Attributes.Add(
" onmouseout " " this.style.backgroundColor=currentcolor " );
        }
    }

 

 

四。数据绑定的其他知识:

绑定方法:Bind Eval 语法:<%# Bind("字段名") %>

字段格式化:{0:D} 设置显示的内容是货币类型 {0:D}设置显示的内容是数字  {0:yy--mm--dd} 设置显示是日期格式

 

五。 经验:在查询所有数据的时候如果实体类存在外键关系 建议使用DataTable或DataSet 获取数据 避免出现 使用SqlDataReader 没有关闭的异常

 

 

ExpandedBlockStart.gif 代码
   ///   <summary>
        
///  通过SQL语句查询所有的书籍
        
///   </summary>
        
///   <param name="sql"></param>
        
///   <returns></returns>
         public   static  List < Book >  getBooksBySQL( string  sql)
        {
            List
< Book >  list  =   new  List < Book > ();
            DataTable dt 
=  DBHelper.GetDataSet(sql);
            
foreach  (DataRow reader  in  dt.Rows)
            {

                Book book 
=   new  Book();
                book.Author 
=  reader[ " author " ].ToString();
                book.AuthorDescription 
=  reader[ " aurhorDescription " ].ToString();
                book.Clicks 
=  ( int )reader[ " Clicks " ];
                book.ContentDescription 
=  ( string )reader[ " ContentDescription " ];
                book.EditorComment 
=  reader[ " EditorComment " ].ToString();
                book.Id 
=  ( int )reader[ " Id " ];
                book.Isbn 
=  reader[ " Isbn " ].ToString();
                book.PublishDate 
=  (DateTime)reader[ " PublishDate " ];
                book.Title 
=  reader[ " Title " ].ToString();
                book.Toc 
=  reader[ " Toc " ].ToString();
                book.UnitPrice 
=  ( decimal )reader[ " UnitPrice " ];
                book.WordsCount 
=  ( int )reader[ " WordsCount " ];

                book.Category 
=  CategoryService.getCategoryByCategoryId(( int )reader[ " categoryid " ]); // PK 外键
                book.Publisher  =  PublisherService.getPublisherByPublisherId(( int )reader[ " publisherid " ]); // PK 外键

                list.Add(book);
            }
            
return  list;
        }

 

 

 

ExpandedBlockStart.gif 代码

        
///   <summary>
        
///  根据分类ID查询分类信息
        
///   </summary>
        
///   <param name="categoryId"></param>
        
///   <returns></returns>
         public   static  Category getCategoryByCategoryId( int  categoryId)
        {
            
string  sql  =   " select * from categories where id = @id " ;
            Category cate 
=   new  Category();
            SqlParameter[] para 
=    new  SqlParameter[]
            {
                
new  SqlParameter( " @id " ,categoryId)
            };
            DataTable table 
=   DBHelper.GetDataSet(sql,para);
            
foreach  (DataRow reader  in  table.Rows)
            {
                cate.Id 
=  ( int )reader[ " id " ];
                cate.Name 
=  ( string )reader[ " name " ];
            }
            
return  cate;
        }

 

 

 

ExpandedBlockStart.gif 代码

        
///   <summary>
        
///  根据出版社编号ID查询出版社信息
        
///   </summary>
        
///   <param name="publisherId"></param>
        
///   <returns></returns>

        
public   static  Publisher getPublisherByPublisherId( int  publisherId)
        {
            
string  sql  =   " select * from publishers where id = @id " ;
            Publisher pub 
=   new  Publisher();
            SqlParameter[] para 
=   new  SqlParameter[]
            {
                
new  SqlParameter( " @id " ,publisherId)
            };
            DataTable table 
=  DBHelper.GetDataSet(sql,para);
            
foreach  (DataRow reader  in  table.Rows)
            {
                pub.Id 
=  ( int )reader[ " id " ];
                pub.Name 
=  ( string )reader[ " name " ];
            }
            
return  pub;
        }

 

 

还未总结完。

转载于:https://www.cnblogs.com/Simcoder/archive/2010/04/09/1708634.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值