RadGrid Wrapper传递一个NeedDataSource回调

本文关键字:一个 NeedDataSource 回调 Wrapper RadGrid | 更新日期: 2023-09-27 17:50:33

我正在尝试编写一个简单的助手类,它将使管理我们应用程序中的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 . dataretriieve ()没有被调用,我得到异常"数据源是无效类型。它必须是IListSource, IEnumerable或IDataSource"",只要行(作为RadGrid)。DataSource = _NeedDataSourceCallBack;在helper类中被击中。所以我猜我没有设置在NeedDataSource事件上的钩子,也没有设置如何调用回调,因为我知道dataretriieve()正在返回一个合适的对象列表。

谁能给我指个正确的方向?

RadGrid Wrapper传递一个NeedDataSource回调

需要按如下方式调用Invoke():

(aSender as RadGrid).DataSource = _NeedDataSourceCallBack.Invoke(aSender);
相关文章: