简体   繁体   中英

How to paint cells in row (Telerik)?

I've next code that handle fowFormatting my cells:

private void gridViewRaces_RowFormatting(object sender, RowFormattingEventArgs e)
    {
        foreach (var cellColumn in e.RowElement.Data.Cells)
        {
            var cellInfo = cellColumn as GridViewCellInfo;
            if (cellInfo != null)
            {
                cellInfo.Style.DrawFill = true;
                if (cellInfo.ColumnInfo.Name == "columnContactProducerName")
                {
                    cellInfo.Style.DrawFill = true;
                    cellInfo.Style.BackColor = Color.Yellow;
                }
                else if (cellInfo.ColumnInfo.Name == "columnTransport")
                {
                    cellInfo.Style.BackColor = Color.Yellow;
                }
                else
                {
                    cellInfo.Style.BackColor = ColorTranslator.FromHtml((e.RowElement.Data.DataBoundItem as RaceForListDto).Color);
                }
            }

        }
        //e.RowElement.BackColor = ColorTranslator.FromHtml((e.RowElement.Data.DataBoundItem as RaceForListDto).Color);
    }

but my cells aren't painting. How to paint some cells in rows on dataBinding?

It looks like the proper event to do this is ItemDataBound event. See here:

http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/appearance-and-styling/conditional-formatting

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
//Is it a GridDataItem
if (e.Item is GridDataItem)
{
    //Get the instance of the right type
    GridDataItem dataBoundItem = e.Item as GridDataItem;

    //Check the formatting condition
    if (int.Parse(dataBoundItem["Size"].Text) > 100)
    {
        dataBoundItem["Received"].ForeColor = Color.Red;
        dataBoundItem["Received"].Font.Bold = true;
        //Customize more...
    }
}
}

Or event better is to use a custom CSS class so that you can later make changes without having to rebuild the project:

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e){
if (e.Item is GridDataItem)
{
    GridDataItem dataItem = e.Item as GridDataItem;
    if (dataItem["Country"].Text == "Mexico")
        dataItem.CssClass = "MyMexicoRowClass";
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM