RadGrid Wrapper传递NeedDataSource回调

时间:2015-05-25 03:04:16

标签: c# telerik telerik-grid

我正在尝试编写一个简单的帮助程序类,它将使我们的应用程序中的Telerik RadGrids一致。

该课程的相关部分如下所示:

public delegate object NeedDataSource(object aSender);

public class TelerikGridViewHelper
{
    private readonly string _CommandNameDelete; //String checked for in ItemCommand to determine if we have a delete button click
    private readonly string _CommandNameEdit; //String checked for in ItemCommand to determine if we have a edit button click
    private readonly RadGrid _GridView;
    private NeedDataSource _NeedDataSourceCallBack;
    private RecordDelete _RecordDeleteCallback;
    private RecordEdit _RecordEditCallback;

    /// <summary>
    ///     Class constructor
    /// </summary>
    /// <param name="aGridView"></param>
    public TelerikGridViewHelper(RadGrid aGridView)
    {
        //Save params supplied
        _GridView = aGridView;

        //Create resources required
        _CommandNameDelete = Guid.NewGuid() + "_Delete";
        _CommandNameEdit = Guid.NewGuid() + "_Edit";
        Options = new TelerikGridViewHelperOptions();
    }

    /// <summary>
    ///     Defines how the grid view will be configured when the Configure() method is called.
    /// </summary>
    public TelerikGridViewHelperOptions Options { get; set; }

    /// <summary>
    /// Call this method after setting the appropriate Options to configure the grid view behaviours.
    /// </summary>
    /// <param name="aKeyFieldNames">List of fields that uniquely identify each record.</param>
    /// <param name="aNeedDataSourceCallback">Method returning the contents of the datasource to be displayed by the grid.</param>
    /// <param name="aRecordDeleteCallback">Called when a record is to be deleted as a consequence of the user pressing the Delete button automatically added by this class when Options.CommandDelete is true.</param>
    /// <param name="aRecordEditCallback">Called when a record is to be edit as a consequence of the user pressing the Edit button automatically added by this class when Options.CommandEdit is true.</param>
    public void Configure(string[] aKeyFieldNames, NeedDataSource aNeedDataSourceCallback, RecordDelete aRecordDeleteCallback, RecordEdit aRecordEditCallback)
    {
        //Save / Apply the parameters supplied
        //Datasource
        _NeedDataSourceCallBack = aNeedDataSourceCallback;
        _RecordDeleteCallback = aRecordDeleteCallback;
        _RecordEditCallback = aRecordEditCallback;

        //Key fields
        _GridView.MasterTableView.DataKeyNames = aKeyFieldNames;

        //Now configure the grid based on the configured behaviours
        ConfigureGrid(_GridView, Options);
    }

    /// <summary>
    ///     Internal helper method to configure the various aspects of the grid based on the options specified.
    /// </summary>
    /// <param name="aGridView">Gridview to configure.</param>
    /// <param name="aOptions">Options to use when configuring the grid.</param>
    private void ConfigureGrid(RadGrid aGridView, TelerikGridViewHelperOptions aOptions)
    {
        //Grid Options
        aGridView.AllowFilteringByColumn = aOptions.Filtering;
        aGridView.AllowPaging = aOptions.Paging;
        aGridView.AutoGenerateColumns = aOptions.AutoGenerateColumns;
        aGridView.AutoGenerateHierarchy = true;
        aGridView.EnableHeaderContextMenu = true;
        aGridView.GroupPanelPosition = GridGroupPanelPosition.Top;
        aGridView.Skin = aOptions.ThemeName;

        //Table view options
        aGridView.MasterTableView.PagerStyle.AlwaysVisible = true;
        aGridView.MasterTableView.PagerStyle.Position = GridPagerPosition.TopAndBottom;
        aGridView.MasterTableView.PageSize = aOptions.PageSize;

        //Events hookup
        _GridView.NeedDataSource += CustomGridViewOnNeedDataSource;
    }

    /// <summary>
    ///     Hooked to the managed grid view's OnNeedDataSource event. We use this to call what the developer has supplied for
    ///     the datasource callback.
    /// </summary>
    /// <param name="aSender">Gridview instance that has fired this event</param>
    /// <param name="aEventArgs">Contains the information about the need for the rebind of the datasource.</param>
    private void CustomGridViewOnNeedDataSource(object aSender, GridNeedDataSourceEventArgs aEventArgs)
    {
        (aSender as RadGrid).DataSource = _NeedDataSourceCallBack;
    }

    /// <summary>
    ///     Internal helper method to raise the exception class associated with this class
    /// </summary>
    /// <param name="aExceptionMessage">Message to use when raising the exception.</param>
    private void RaiseException(string aExceptionMessage)
    {
        throw new TelerikGridViewHelperException(aExceptionMessage);
    }
}

然后从后面的代码调用该类,如下所示:

 public partial class TimeSheetList : BasePage
    {
        private readonly TimeSheetProcess _TimeSheetProcess;

        /// <summary>
        ///     Class constructor
        /// </summary>
        public TimeSheetList()
        {
            _TimeSheetProcess = new TimeSheetProcess();
        }

        /// <summary>
        ///     Called when the page is loaded
        /// </summary>
        /// <param name="aSender"></param>
        /// <param name="aEventArgs"></param>
        protected void Page_Load(object aSender, EventArgs aEventArgs)
        {

            TelerikGridViewHelper gridViewHelper = new TelerikGridViewHelper(RadGrid2);
            gridViewHelper.Configure(new []{"TimeSheetId"}, DataRetrieve, null, null);
        }

        private object DataRetrieve(object aSender)
        {
            //List of timesheets ordered by date and then user within that
            DateTime todaysDate = DateTime.Now;

            //List of page contents ordered by url
            List<Timesheet> timesheetsFromDb = _TimeSheetProcess.GetTimeSheets()
                .ToList();

            MembershipCache membershipCache = new MembershipCache();
            ClockedInBrowseViewModel timesheetsViewModel = new ClockedInBrowseViewModel();

            foreach (Timesheet timeSheet in timesheetsFromDb)
            {
                MembershipUser userInfo = membershipCache.GetUserById(timeSheet.UserId.ToString());

                timesheetsViewModel.Items.Add(new ClockedInItemViewModel
                {
                    ClockedInAt = timeSheet.Start.HasValue ? string.Format("{0:dd MMM HH:mm}", timeSheet.Start) : "-",
                    ClockedOutAt = timeSheet.Finish.HasValue ? string.Format("{0:dd MMM HH:mm}", timeSheet.Finish) : "-",
                    TimesheetId = timeSheet.TimesheetID,
                    Username = userInfo.UserName
                });
            }

            return timesheetsViewModel.Items;
        }
    }

现在我遇到的问题是传入的 TimeSheetList.DataRetrieve()没有被调用,我得到异常&#34;数据源是无效的类型。一旦&#34;(aSender as RadGrid).DataSource = _NeedDataSourceCallBack;&#34; 被击中,它必须是IListSource,IEnumerable或IDataSource&#34; 在辅助类中。所以我猜我没有设置NeedDataSource事件的挂钩或者如何调用回调,因为我知道DataRetrieve()正在返回一个合适的对象列表。

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

需要调用Invoke(),如下所示:

(aSender as RadGrid).DataSource = _NeedDataSourceCallBack.Invoke(aSender);