如何创建新的gridview列类型

时间:2013-01-21 23:29:36

标签: c# asp.net .net custom-controls

我正在为我的ASP.NET APP创建一个自定义gridview控件,以及我希望它创建一个新的自定义类型列的其中一项,以便执行以下操作:

(这就是现在创建列的方式)

<asp:TemplateField>
<HeaderTemplate>
    <asp:LinkButton Text="R. Name" ToolTip="Resource Name" CommandName="Sort" CommandArgument="ResourceName"
        runat="server" />
    <uc:GridViewFilter ID="ucGridViewFilterResourceName" ColumnName="ResourceName" AssociatedControlType="TextBoxString"
        OnFilterApplied="ucGridViewFilter_FilterApplied" runat="server" />
</HeaderTemplate>
<ItemTemplate>
    <%# Eval("ResourceName") %>
</ItemTemplate>

我想有这样的事情:

<asp:GridViewExColumn HeaderTitle="R. Name" HeaderToolTip="Resource Name" ColumnName="ResourceName" SearchType="TextBoxString" OnFilterApplied="ucGridViewFilter_FilterApplied" Text='<%# Eval("ResourceName") %>' />

任何人都可以在隧道尽头显示一些光线?我完全迷失了,我可以成功创建自定义gridview,但不知道从这个自定义列类型开始。感谢

1 个答案:

答案 0 :(得分:0)

我终于找到了做我想要的方法,我会为你提供我的创始人,也许你甚至可以做得更好。

我创建了一个新的.cs扩展控件DataControlField,如下所示:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using VMPortal.Tools;

public class GridViewExColumn : DataControlField
{
    public string HeaderToolTip { get; set; }
    public string GridViewID { get; set; }
    public string SearchType { get; set; }
    public string DataField
    {
        get
        {
            object value = ViewState["DataField"];

            if (value != null)
                return value.ToString();

            return string.Empty;
        }

        set
        {
            ViewState["DataField"] = value;
            OnFieldChanged();
        }
    }

    protected override DataControlField CreateField()
    {
        return new BoundField();
    }

    public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
    {
        base.InitializeCell(cell, cellType, rowState, rowIndex);

        if (cellType == DataControlCellType.Header)
        {
            var lb = new LinkButton
            {
                Text = HeaderText,
                ToolTip = String.IsNullOrWhiteSpace(HeaderToolTip) ? HeaderText : HeaderToolTip,
                CommandName = "Sort",
                CommandArgument = DataField
            };

            var tt = cell.Controls;
            cell.Controls.Add(lb);

            if (!String.IsNullOrWhiteSpace(SearchType))
            {
                // Add filter control
                var ctrlFilter = Control.Page.LoadControl("~/Controls/GridViewFilter.ascx") as GridViewFilter;
                cell.Controls.Add(ctrlFilter);
                ctrlFilter.ID = GridViewID + DataField;
                ctrlFilter.ColumnName = DataField;
                ctrlFilter.AssociatedControlType = SearchType;
                ctrlFilter.FilterApplied += ctrlFilter_FilterApplied;
            }
        }
        else if (cellType == DataControlCellType.DataCell)
            cell.DataBinding += new EventHandler(cell_DataBinding);
    }

    protected void cell_DataBinding(object sender, EventArgs e)
    {
        var cell = (TableCell)sender;
        var dataItem = DataBinder.GetDataItem(cell.NamingContainer);
        var dataValue = DataBinder.GetPropertyValue(dataItem, DataField);
        string value = dataValue != null ? dataValue.ToString() : "";
        cell.Text = value;
    }

    protected void ctrlFilter_FilterApplied(object sender, EventArgs e)
    {
        var filterExpression = sender as FilterExpression;
        if (filterExpression != null)
        {
            if (FilterApplied != null)
                FilterApplied(null, EventArgs.Empty);
        }
    }

现在我可以添加一个新列,如下面的

    <uc:GridViewExColumn HeaderText="R. Name" HeaderToolTip="Resource Name" DataField="ResourceName" SearchType="TextBoxString" GridViewID="ucGridViewEx" OnFilterApplied="ucGridViewFilter_FilterApplied" />

希望它可以帮助别人。