带搜索功能的GridView控件

本文介绍了一个带有搜索功能的GridView扩展控件,包括如何添加序列号列、在Footer中添加搜索功能和处理搜索事件。通过设置属性,可以实现对数据行的搜索、排序和显示总行数。此外,提供了事件处理方法示例,帮助读者理解如何在实际项目中使用此控件。
摘要由CSDN通过智能技术生成

  带搜索功能的GridView控件 收藏
Demo代码下载

最近需要一个gridview能够有不错的搜索功能,最好还是个GridView的扩展控件以后可以直接使用。我试着在CSDN上搜了一下下面这两篇文章都是带Search功能的GridView。

http://blog.csdn.net/xiandawang/archive/2007/05/25/1625529.aspx

http://blog.csdn.net/xiandawang/archive/2007/05/25/1625529.aspx

但是它们不是很好。后来终于看到一个SearchGridview控件,挺不错的介绍给大家。

 

 

我这直接介绍一下它的实现吧。

1.       写了一个GridView的扩展控件SearchabbleGridView类。

2.       添加一个TemplateColumn列来显示数据的条数。

3.       在GridView的footer添加一个搜索功能。

4.       当点击那个搜索按钮时触发一个事件。

代码部分:

1.       GridView的扩展类:

view plaincopy to clipboardprint?
public class SearchGridView : GridView 
public class SearchGridView : GridView

2.       创建一个模板列来显示每行记录的序列号。

       view plaincopy to clipboardprint?
public class NumberColumn : ITemplate  
 
{  
 
    public void InstantiateIn(Control container)  
 
    {  
 
   
 
    }  
 

public class NumberColumn : ITemplate

{

    public void InstantiateIn(Control container)

    {

 

    }

}

 

3.       在SearchableGrivView中我重载了OnInite方法来添加序列号的列为GridView的第一列。这里有个标志位ShowRowNumber如果是true的话才显示序列号列。

       view plaincopy to clipboardprint?
protected override void OnInit(EventArgs e)  
 
{  
 
    base.OnInit(e);  
 
    //If showrownumber option is turned on then add   
 
    //the template column as the first column.  
 
    if (!IsDesign() && ShowRowNumber)   
 
    {  
 
        TemplateField tmpCol = new TemplateField();  
 
        NumberColumn numCol = new NumberColumn();  
 
        tmpCol.ItemTemplate = numCol;  
 
        // Insert this as the first column  
 
        this.Columns.Insert(0, tmpCol);  
 
    }  
 

protected override void OnInit(EventArgs e)

{

    base.OnInit(e);

    //If showrownumber option is turned on then add

    //the template column as the first column.

    if (!IsDesign() && ShowRowNumber)

    {

        TemplateField tmpCol = new TemplateField();

        NumberColumn numCol = new NumberColumn();

        tmpCol.ItemTemplate = numCol;

        // Insert this as the first column

        this.Columns.Insert(0, tmpCol);

    }

}

 

4.       方法OnRowCreated在每一行被创建的时候会被调用。通过判断当前行的RowType可以在Footer中添加总的行数和搜索模块。

      view plaincopy to clipboardprint?
protected override void OnRowCreated(GridViewRowEventArgs e)  
 
{  
 
    base.OnRowCreated(e);  
 
    if (!IsDesign()) //During Runtime  
 
    {  
 
        if (e.Row.RowType == DataControlRowType.Footer)  
 
        {  
 
            //If ShowFooter is set to true  
 
            if (ShowFooter && e.Row.Cells.Count > 0)  
 
            {  
 
                //If TotalRows has to be shown  
 
                if (ShowTotalRows)  
 
                {  
 
                    e.Row.Cells[0].Text = ViewState[NO_OF_ROWS] + " Rows.";  
 
                }  
 
                if (e.Row.Cells[e.Row.Cells.Count - 1].Controls.Count == 0)  
 
                {  
 
                    //Create the search control  
 
                    Table table = new Table();  
 
                    table.Style.Add("width", "100%");  
 
                    table.Style.Add("align", "right");  
 
                    TableRow tr = new TableRow();  
 
                    TableCell tc = new TableCell();  
 
                    tc.Style.Add("align", "right");  
 
                    tc.Style.Add("width", "100%");  
 
   
 
                    //Populate the dropdownlist with the Ids  
 
                    //of the columns to be filtered  
 
                    if (_ddlFinder.Items.Count == 0)  
 
                        SetFilter();  
 
   
 
                    _btnSearch.Width = 20;  
 
                    _btnSearch.Height = 20;  
 
                    _btnSearch.ImageAlign = ImageAlign.AbsMiddle;  
 
      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值