如何将 Cell Formatting 事件方法添加到动态创建的 radgrid 代码后面?

How to add Cell Formatting event method to dynamically created radgrid code behind?

我在代码隐藏上创建了一个 rad 网格。现在我想向它添加单元格格式化事件。我怎样才能做到这一点?

       RadGrid grid = new RadGrid();
        grid.ID = "rdggrid";

        grid.Skin = "Metro";
        grid.Width = Unit.Percentage(100);            
        grid.PageSize = 15;
        grid.AllowPaging = true;
        grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
        grid.AutoGenerateColumns = false;          


        grid.MasterTableView.Width = Unit.Percentage(100);

        grid.ClientSettings.Resizing.AllowColumnResize = true;
        grid.ClientSettings.Resizing.ResizeGridOnColumnResize = true;
        grid.ClientSettings.Resizing.AllowResizeToFit = true;

        grid.ItemDataBound += new GridItemEventHandler(RadGrid_ItemDataBound);

我要添加这个方法

void radGrid_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)     
{     

}

RadGridView for WinForms 不同,ASP.NET AJAX (WebForms) 的 RadGrid 不提供 CellFormatting 服务器端事件。您可以使用 RadGrid 的 ItemDataBound 事件 ASP.NET AJAX 来设置单元格的样式:

 protected void RadGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            if (Convert.ToInt32(((DataRowView)item.DataItem)["Column"]) < value)
            {
                TableCell cell = item["Column"];
                cell.BackColor = Color.PeachPuff;
            }
        }
    }

protected void RadGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        foreach (GridDataItem dataItem in RadGridProduct.MasterTableView.Items)
        {
            int cellCount = dataItem.Cells.Count;

            foreach (GridTableCell item in dataItem.Cells)
            {
                if (item.Text == null ||Convert.ToInt32(item.Text) < 0 )
                    item.BackColor = System.Drawing.Color.Brown;
            }

        }

    }