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

        通过前面两篇文章的学习,大家对自定义列控件的基本知识都掌握了,本节为大家巩固下前面学习的东西,以上篇文章为基础,扩展审查列控件,使它能审查多个单词。
        我们通过把要检查的单词和替换的单词保存在XML文件中,这样便于修改。
        
        XML文件如下(Text.xml):
     
None.gif <? xml version="1.0" encoding="utf-8"  ?>  
None.gif
< censors >  
None.gif  
< censor >  
None.gif    
< find > wit </ find >  
None.gif    
< replace > w*t </ replace >  
None.gif  
</ censor >  
None.gif  
< censor >  
None.gif    
< find > ra </ find >  
None.gif    
< replace > *a </ replace >  
None.gif  
</ censor >  
None.gif
</ censors >  

        完整代码如下:
       
None.gif      public   class  CensorColumn :DataGridColumn
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private string m_DataFiled;
InBlock.gif        
public string DataField
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_DataFiled;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//检查文本
InBlock.gif
        private string m_XmlFile;
InBlock.gif        
public string XmlFile
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_XmlFile;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.InitializeCell (cell, columnIndex, itemType);
InBlock.gif            
if((itemType == ListItemType.AlternatingItem)||(itemType == ListItemType.SelectedItem)||(itemType == ListItemType.Item))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cell.DataBinding 
+= new EventHandler(PerformDataBinding);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void PerformDataBinding(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            TableCell cell 
= (TableCell)sender;
InBlock.gif            DataGridItem gridItem 
= (DataGridItem)cell.NamingContainer;
InBlock.gif            Object dataItem 
= gridItem.DataItem;
InBlock.gif
InBlock.gif            
if(!this.m_DataFiled.Equals(string.Empty))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cell.Text 
= PerformShip((string)DataBinder.Eval(dataItem, DataField));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string PerformShip(string text)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(m_XmlFile.Equals(string.Empty))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return text;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return PerformXml(text);
ExpandedSubBlockEnd.gif            }
    
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string PerformXml(string text)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string file = HttpContext.Current.Server.MapPath(this.XmlFile);
InBlock.gif            
if(!File.Exists(file))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return text;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                XmlDocument doc 
= new XmlDocument();
InBlock.gif                doc.PreserveWhitespace 
= true;
InBlock.gif                doc.Load(file);
InBlock.gif                XmlNode node 
= doc.DocumentElement;    
InBlock.gif                XmlNodeList findNodes 
= node.SelectNodes("/censors/censor/find");        
InBlock.gif                XmlNodeList replaceNodes 
= node.SelectNodes("/censors/censor/replace");
InBlock.gif                
int i;
InBlock.gif                
for(i=0;i<findNodes.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    text 
= text.Replace(findNodes.Item(i).InnerText,replaceNodes.Item(i).InnerText);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return text;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif    }

None.gif

        在DataGrid中添加列控件:
   
None.gif                  < Columns >
None.gif                    
< custcols:CensorColumn  XmlFile ="Text.xml"  DataField ="ShipCountry" ></ custcols:CensorColumn >
None.gif                
</ Columns >

    效果图: pic4.JPG

        
        一口气写完了三篇,相信大家看完后掌握了自定义列控件开发的基本知识。这三篇文章都是从很基础的角度讲述列控件的开发,如果你想要进一步的提高,可以看看lovecherry的这篇文章 http://lovecherry.cnblogs.com/lovecherry/archive/2005/05/01/148504.html

转载于:https://www.cnblogs.com/jierry/archive/2005/10/28/263999.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值