views:

984

answers:

2

Can anyone help me in creating Custom GridView Control in c# .Net?

A: 

We'd need to know something about what you wanted your GridView to support that isn't possible in the standard one.

ChrisF
I want the gridview to perform sorting and filtering as per the configuration when the data is bound.
ChrisF
Am working with VS2008, here i have selected ASP.Net Server Control template to create CustomControl.By default class is inherited from WebControl, how can i inherit the class from GridView control?
Edit the code to change "WebControl" to "GridVew" as a start.
ChrisF
Hi, I have to create a CustomGridView control which derives from GridView. I have to write a method which accepts a list(datasource) and the data should be sorted and filtered before doing base.DataBind(). This is what is my requirement, how can i do this?
+1  A: 

Here's a blog post and a code sample (from one of my projects) that may give you what you need.

ASP.NET 2.0 - Extending GridView control to display extra Footer Rows

public class MyGridView : GridView
{
    protected GridViewRow _footerRow = null;

    public override GridViewRow FooterRow
    {
        get
        {
            if (_footerRow == null)
            {
                return base.FooterRow;
            }
            else
            {
                return _footerRow;
            }
        }
    }
    public MyGridView()
    {
    }

    protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
    {
        ...


        ...
    }



    [System.ComponentModel.Category("Behavior")]
    [Themeable(true)]
    [System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.No)]
    public bool ShowHeaderWhenEmpty
    {
        get
        {
            if (this.ViewState["ShowHeaderWhenEmpty"] == null)
            {
                this.ViewState["ShowHeaderWhenEmpty"] = false;
            }

            return (bool)this.ViewState["ShowHeaderWhenEmpty"];
        }
        set
        {
            this.ViewState["ShowHeaderWhenEmpty"] = value;
        }
    }

    [System.ComponentModel.Category("Behavior")]
    [Themeable(true)]
    [System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.No)]
    public bool ShowFooterWhenEmpty
    {
        get
        {
            if (this.ViewState["ShowFooterWhenEmpty"] == null)
            {
                this.ViewState["ShowFooterWhenEmpty"] = false;
            }

            return (bool)this.ViewState["ShowFooterWhenEmpty"];
        }
        set
        {
            this.ViewState["ShowFooterWhenEmpty"] = value;
        }
    }
}
Tone
I want the gridview to perform sorting and filtering as per the configuration when the data is bound