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

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

        完整代码如下:
       
    public class CensorColumn :DataGridColumn
    
{
        
private string m_DataFiled;
        
public string DataField
        
{
            
get
            
{
                
return this.m_DataFiled;
            }

            
set
            
{
                
this.m_DataFiled = value;
            }

        }


        
//检查文本
        private string m_XmlFile;
        
public string XmlFile
        
{
            
get
            
{
                
return this.m_XmlFile;
            }

            
set
            
{
                
this.m_XmlFile = value;
            }

        }


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

        }


        
private void PerformDataBinding(object sender, System.EventArgs e)
        
{
            TableCell cell 
= (TableCell)sender;
            DataGridItem gridItem 
= (DataGridItem)cell.NamingContainer;
            Object dataItem 
= gridItem.DataItem;

            
if(!this.m_DataFiled.Equals(string.Empty))
            
{
                cell.Text 
= PerformShip((string)DataBinder.Eval(dataItem, DataField));
            }

        }


        
private string PerformShip(string text)
        
{
            
if(m_XmlFile.Equals(string.Empty))
            
{
                
return text;
            }

            
else 
            
{
                
return PerformXml(text);
            }
    
        }


        
private string PerformXml(string text)
        
{
            
string file = HttpContext.Current.Server.MapPath(this.XmlFile);
            
if(!File.Exists(file))
            
{
                
return text;
            }

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

                
return text;
            }

        }


    }


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

    效果图:

        
        一口气写完了三篇,相信大家看完后掌握了自定义列控件开发的基本知识。这三篇文章都是从很基础的角度讲述列控件的开发,如果你想要进一步的提高,可以看看lovecherry的这篇文章http://lovecherry.cnblogs.com/lovecherry/archive/2005/05/01/148504.html
posted @ 2005-10-28 17:25  jierry  阅读(1630)  评论(2编辑  收藏  举报