为DataGrid创建自定义列控件(四)

        全选和多选的功能在DataGrid中使用的时候很多,以前我们都是创建CheckBox模板列,然后在后台中捕获ChecnkBox的选择情况来实现全选或多选.现在为了加快开发的速度,使用这个CheckBoxColumn列控件,可以很方便的实现多选或全选的功能.
        代码如下:
       
ContractedBlock.gif ExpandedBlockStart.gif CheckBoxColumn
None.gifpublic class CheckBoxColumn : DataGridColumn
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private String _strId=String.Empty;
InBlock.gif    
InBlock.gif        
public CheckBoxColumn(): base()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          
InBlock.gif            
//重写父类InitializeCell方法
InBlock.gif
            base.InitializeCell(cell, columnIndex, itemType);
InBlock.gif                        
InBlock.gif            
//加入多选框
InBlock.gif
            if(itemType == ListItemType.EditItem || itemType == ListItemType.Item || itemType == ListItemType.AlternatingItem || itemType == ListItemType.SelectedItem)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                HtmlInputCheckBox checkbox 
= new HtmlInputCheckBox();
InBlock.gif                
//可以自定义ID
InBlock.gif
                if(_strId==String.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    checkbox.Name 
= "checkboxCol";
InBlock.gif                    checkbox.ID 
= "checkboxCol";
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    checkbox.Name 
= myID;
InBlock.gif                    checkbox.ID
=myID;
ExpandedSubBlockEnd.gif                }

InBlock.gif        
InBlock.gif                cell.Controls.Add(checkbox);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private String CreateName()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Random rnd
=new Random(0);
InBlock.gif            String _strNameValue
=Convert.ToString((int)rnd.Next(100));
InBlock.gif            
return _strNameValue;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private String myID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _strId;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif        
public String ID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _strId
=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override string HeaderText
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return base.HeaderText;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public override TableItemStyle HeaderStyle
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                TableItemStyle t
=new TableItemStyle();
InBlock.gif                t.HorizontalAlign
=HorizontalAlign.Center;
InBlock.gif                
return t;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
//获得选中的Index值
InBlock.gif
        public Int32[] SelectedIndexes 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                ArrayList selectedIndexList 
= new ArrayList();
InBlock.gif                
//获得DataGrid中的选择框
InBlock.gif
                foreach( DataGridItem item in this.Owner.Items ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    HtmlInputCheckBox chkBox
=null;
InBlock.gif                    
if(_strId==String.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        chkBox 
= (HtmlInputCheckBox) item.FindControl("checkboxCol");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        chkBox 
= (HtmlInputCheckBox) item.FindControl(myID);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
InBlock.gif                    
//如果选中,就把选中的Index值放入ArrayList中
InBlock.gif
                    if ( chkBox != null && chkBox.Checked )  
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        selectedIndexList.Add( item.ItemIndex );
ExpandedSubBlockEnd.gif                    }
 
InBlock.gif                    
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return (Int32[])selectedIndexList.ToArray(typeof( System.Int32 ) );
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//获得未选中的Index值
InBlock.gif
        public Int32[] UnSelectIndexes
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ArrayList UnSelectIndexList
=new ArrayList();
InBlock.gif                
foreach(DataGridItem item in this.Owner.Items)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    HtmlInputCheckBox chkBox
=null;
InBlock.gif                    
if(_strId==String.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        chkBox
=(HtmlInputCheckBox)item.FindControl("checkboxCol");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        chkBox
=(HtmlInputCheckBox)item.FindControl(myID);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
//If it's not selected then add it to the arraylist
InBlock.gif
                    if(chkBox!=null&&chkBox.Checked==false)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        UnSelectIndexList.Add(item.ItemIndex);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
return (Int32[])UnSelectIndexList.ToArray(typeof(System.Int32));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//获得选中的DataKeys值
InBlock.gif
        public object[] SelectedDataKeys 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ArrayList dataKeyList 
= new ArrayList();
InBlock.gif                
if(this.Owner.DataKeys.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
foreach( Int32 selectedIndex in SelectedIndexes ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{            
InBlock.gif                        
object DataKey = (this.Owner.DataKeys[selectedIndex].ToString());
InBlock.gif                        dataKeyList.Add(DataKey);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
return (object[])dataKeyList.ToArray(typeofobject ) );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif
InBlock.gif        
//获得未选中的DataKeys值
InBlock.gif
        public object[] UnSelectDataKeys
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ArrayList UnSelDataKeyList
=new ArrayList();
InBlock.gif                
if(this.Owner.DataKeys.Count>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
foreach(Int32 unSelectIndex in UnSelectIndexes)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
object DataKey=(this.Owner.DataKeys[unSelectIndex].ToString());
InBlock.gif                        UnSelDataKeyList.Add(DataKey);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
return (object[])UnSelDataKeyList.ToArray(typeof(object));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif            }
        
        看过我前面几篇列控件介绍的朋友对上面的代码一定不会有什么问题.
        首先还是在DataGrid中引用这个列控件,方法和以前一样,这里就不多说了.
        然后看看具体的使用:
  1. 获得选择行的Index值
    None.gif CheckBoxColumn chkColumn  =  (CheckBoxColumn) this .DataGrid1.Columns[ 0 ];
    None.gif            
    foreach ( object  index  in  chkColumn.SelectIndexes) // SelectIndexes获得选择的Index值
    ExpandedBlockStart.gifContractedBlock.gif
                 dot.gif {
    InBlock.gif                Response.Write(index.ToString()
    +"<br>");
    ExpandedBlockEnd.gif            }
  2. 获得选择行的DataKeys值
    None.gif CheckBoxColumn chkColumn  =  (CheckBoxColumn) this .DataGrid1.Columns[ 0 ];
    None.gif            
    foreach ( object  index  in  chkColumn.SelectedDataKeys) // SelectedDataKeys获得选择的DataKeys值
    ExpandedBlockStart.gifContractedBlock.gif
                 dot.gif {
    InBlock.gif                Response.Write(index.ToString()
    +"<br>");
    ExpandedBlockEnd.gif            }
  3. 获得未选择行的Index值和获得未选择行的DataKeys值
    None.gif CheckBoxColumn chkColumn  =  (CheckBoxColumn) this .DataGrid1.Columns[ 0 ];
    None.gif            
    foreach ( object  index  in  chkColumn.UnSelectIndexes)
    ExpandedBlockStart.gifContractedBlock.gif            
    dot.gif {
    InBlock.gif                Response.Write(index.ToString()
    +"<br>");
    ExpandedBlockEnd.gif            }

    None.gif
    None.gif
    None.gif
    None.gifCheckBoxColumn chkColumn 
    =  (CheckBoxColumn) this .DataGrid1.Columns[ 0 ];
    None.gif            
    foreach ( object  index  in  chkColumn.UnSelectedDataKeys)
    ExpandedBlockStart.gifContractedBlock.gif            
    dot.gif {
    InBlock.gif                Response.Write(index.ToString()
    +"<br>");
    ExpandedBlockEnd.gif            }
  4. 全选/取消全选
    None.gif foreach (DataGridItem item  in   this .DataGrid1.Items)
    ExpandedBlockStart.gifContractedBlock.gif            
    dot.gif {
    InBlock.gif                HtmlInputCheckBox chkBox
    =(HtmlInputCheckBox)item.FindControl("checkboxCol");
    InBlock.gif                chkBox.Checked 
    = true;        
    ExpandedBlockEnd.gif            }

    None.gif
    // 如果你自定义了列控件的ID,"checkboxCol"换成自定义的ID值
  5. 获得选择行的任意列的
    None.gif             CheckBoxColumn chkColumn  =  (CheckBoxColumn) this .DataGrid1.Columns[ 0 ];
    None.gif            
    foreach ( object  index  in  chkColumn.SelectIndexes)
    ExpandedBlockStart.gifContractedBlock.gif            
    dot.gif {
    InBlock.gif                Response.Write(DataGrid1.Items[(
    int)index].Cells[1].Text);
    ExpandedBlockEnd.gif            }

        基本的使用就介绍完了,都非常的简单.当然我们可以在这个列控件的基础上扩展新的功能,比如在Head加入全选/取消选择框

           

None.gif if (itemType  ==  ListItemType.Header)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                CheckBox headerCheckBox 
= new CheckBox();
InBlock.gif                headerCheckBox.ID 
= "chkAll";
InBlock.gif                headerCheckBox.CheckedChanged 
+= new EventHandler(this.headerCheckBox_CheckedChanged);
InBlock.gif                headerCheckBox.AutoPostBack 
= true;
InBlock.gif                headerCheckBox.Text 
= "全选/取消";
InBlock.gif                cell.Controls.Add(headerCheckBox);
ExpandedBlockEnd.gif            }

None.gif
None.gif
private   void  headerCheckBox_CheckedChanged( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
foreach (DataGridItem item in this.Owner.Items)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//iterate each DataGridItem and find our checkbox
InBlock.gif
                HtmlInputCheckBox chkBox = (HtmlInputCheckBox) item.FindControl("checkboxCol");
InBlock.gif                
//now set that checkboxCol value = to selected
InBlock.gif
                if(chkBox.Checked == false)
InBlock.gif                    chkBox.Checked 
= true;
InBlock.gif                
else
InBlock.gif                    chkBox.Checked 
= false;
InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

   

转载于:https://www.cnblogs.com/jierry/archive/2005/11/07/270804.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值