How to format radgrid cell programmatically
Asked Answered
D

3

5

I have to format (backcolor, forecolor, font style) a radgrid's cells depending on the value of the cell.

For example If the value is negative set the fore color of that cell as red.

Can any one please tell me how this can be achieved?

Deformation answered 25/8, 2009 at 22:53 Comment(0)
V
8
 protected void grdName_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;
            }
        }
    }
Valueless answered 20/6, 2011 at 20:55 Comment(2)
Don't know why someone downvoted me, my answer is different from the other, so I thought I should add it.Valueless
Perhaps they didn't like PeachPuffOccasionally
B
4

Add the line onItemDataBound="Data_OnitemDataBound" to your radGrid declaration in your aspx page.

Then add this to your code behind. The number in the Cells[] is the index of the column you want to modify or validate against.

protected void Data_OnItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;
        if (Convert.ToDecimal(item.Cells[3].Text) < 0)
        {
            item.Cells[3].ForeColor = System.Drawing.Color.Red;
        }
    }
}
Bushore answered 28/8, 2009 at 15:15 Comment(0)
T
2

Below code can be used for all the cells in the RadGrid.

  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;
            }

        }

    }
Trenttrento answered 15/7, 2016 at 6:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.