扩展GridView添加checkbox

我们在用GridView时,通常都要为GridView添加CHECKBOX这一列,所以把它扩展来,比较方便
下面在扩展用到了ITemplate这个接口,这个接口每次都会调用InstantiateIn这个方法,所以我们在这个方法上
实现一些加入checkbox的操作.
代码如下:
ContractedBlock.gif ExpandedBlockStart.gif Code
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.ComponentModel;
None.gif
using System.Collections;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI;
None.gif
using System.Drawing;
None.gif
None.gif
None.gif
namespace WebClass
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
InBlock.gif    
public class GridViewOfCheckBox:GridView
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public enum CustomColumnAlign
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Left, Right
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
private CustomColumnAlign _align = CustomColumnAlign.Left;
InBlock.gif        
private string _checkBoxID="CheckBox1";
InBlock.gif        
private Color _mouseOverColor;
InBlock.gif        
private DataControlFieldCollection _columnCol = new DataControlFieldCollection();
InBlock.gif
InBlock.gif        [Browsable(
true)]
InBlock.gif        [Description(
"所建立的checkBox所在对齐方式")]
InBlock.gif        [Category(
"扩展")]
InBlock.gif        
public CustomColumnAlign Align
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _align;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _align 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Browsable(
true)]
InBlock.gif        [Description(
"所建立的checkBoxID")]
InBlock.gif      
//  [DefaultValue("CheckBox1")]
InBlock.gif
        [Category("扩展")]
InBlock.gif        
public string CheckBoxID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _checkBoxID;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _checkBoxID 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif            
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif  
鼠标经过行时的颜色#region 鼠标经过行时的颜色
InBlock.gif            [Browsable(
true)]
InBlock.gif            [Description(
"鼠标经过行的颜色")]
InBlock.gif            [DefaultValue(
"")]
InBlock.gif            [Category(
"扩展")]
InBlock.gif            
public Color MouseOverColor
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return _mouseOverColor;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
set
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    _mouseOverColor 
= value;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
protected override void OnRowDataBound(GridViewRowEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (!_mouseOverColor.IsEmpty)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (e.Row.RowType == DataControlRowType.DataRow)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//鼠标经过时,行背景色变 
InBlock.gif
                        e.Row.Attributes.Add("onmouseover""c=this.style.backgroundColor;this.style.backgroundColor='#" + this._mouseOverColor.ToArgb().ToString("X").Substring(26+ "'");
InBlock.gif                        
//        //当鼠标移开时还原背景色
InBlock.gif
                        e.Row.Attributes.Add("onmouseout""this.style.backgroundColor=c");
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
base.OnRowDataBound(e);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif  
#endregion

InBlock.gif
InBlock.gif        
protected override ICollection CreateColumns(PagedDataSource dataSource, bool useDataSource)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ArrayList col 
= base.CreateColumns(dataSource, useDataSource) as ArrayList;
InBlock.gif            CreatCheckBox(col);
InBlock.gif            _columnCol.Clear();
InBlock.gif            
foreach (DataControlField c in col)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _columnCol.Add(c);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return col;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public void CreatCheckBox(ArrayList col)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            TemplateField checkColumn 
= new TemplateField();
InBlock.gif            checkColumn.ItemStyle.HorizontalAlign 
= HorizontalAlign.Center;
InBlock.gif            checkColumn.HeaderStyle.HorizontalAlign 
= HorizontalAlign.Center;
InBlock.gif            CheckBoxItemTemplate cbItemTemplate 
= new CheckBoxItemTemplate();
InBlock.gif            CheckBoxItemTemplate.CheckBoxItemId
=_checkBoxID;
InBlock.gif            checkColumn.ItemTemplate 
= cbItemTemplate;
InBlock.gif            CheckBoxHeaderTemplate cbHeaderTemplate 
= new CheckBoxHeaderTemplate();
InBlock.gif            checkColumn.HeaderTemplate 
= cbHeaderTemplate;
InBlock.gif            
if (Align == CustomColumnAlign.Left)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                col.Insert(
0, checkColumn);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (Align == CustomColumnAlign.Right)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                col.Add(checkColumn);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif      
InBlock.gif
InBlock.gif        
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class CheckBoxItemTemplate : ITemplate
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private static string _CheckBoxID;
InBlock.gif
InBlock.gif        
public static string CheckBoxItemId
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _CheckBoxID;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _CheckBoxID 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void InstantiateIn(Control container)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CheckBox cb 
= new CheckBox();
InBlock.gif            cb.ID 
= _CheckBoxID;
InBlock.gif            container.Controls.Add(cb);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
public class CheckBoxHeaderTemplate : ITemplate
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public void InstantiateIn(Control container)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Label la
=new Label();
InBlock.gif            la.Text
="选择";
InBlock.gif            container.Controls.Add(la);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/12384610/archive/2008/02/20/1075609.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值