This is a migrated thread and some comments may be shown as answers.

Data binding Formatting the Cells

1 Answer 146 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Khizar Khan
Top achievements
Rank 1
Khizar Khan asked on 15 Jun 2011, 05:12 PM
Hi,

I have a requirement where I have to compare dates which are in the binded data set to grid. The priority column has to be set to a value and format the cell color to red/orange/gree based on the value of Targetdt and warningdate which is available in the datasource binded to the grid.

I am doing the following.
void rGridViewTasks_ViewCellFormatting(object sender, CellFormattingEventArgs e)
        {
                if (e.CellElement.ColumnInfo.Name == "Priority")
            {
                //if ((DateTime)e.CellElement.RowInfo.Cells["TargetDt"].Value >= DateTime.Now )
                //{
                //    e.CellElement.BackColor = Color.DarkRed;
                //    e.CellElement.Value = "1";
                //}
                //else if (((DateTime)e.CellElement.RowInfo.Cells["WarningDate"].Value >= DateTime.Now) &&
                //     ((DateTime)e.CellElement.RowInfo.Cells["TargetDt"].Value < DateTime.Now))
                //{
                //    e.CellElement.BackColor = Color.DarkOrange;
                //    e.CellElement.Value = "2";
                //}
                //else
                //{
                //    e.CellElement.BackColor = Color.DarkGreen;
                //    e.CellElement.Value = "3";
                //}
            }

}

the above WarningDate and TargetDt columns are not added as the columns in the grid, hence they are coming as null and giving a exception.

I checked the rowinfo databounditem and it is also null. Please advice.

regards,
Khizar

1 Answer, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 20 Jun 2011, 03:34 PM
Hello Khizar,

Thank you for your question.

You can accomplish your scenario adding the TargetDt and WarningDate columns as invisible in RadGridView:

this.radGridView1.Columns["TargetDt"].IsVisible = false;

Then you can use their values in formatting events.

Keep in mind that ViewCellFormatting event is raised for every RadGridView cell. I suppose you do not aim to format, for example, the header row and the filter row depending on values in your hidden columns. I would advise you to format the data cells of the Priority column using the CellFormatting event of RadGridView.

The CellFormatting event is raised for data cells and new row cells. As your hidden columns do not have values for the new row, you should execute your formatting logic only for data rows:

private void rGridViewTasks_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement.RowElement is GridNewRowElement)
    {
        return;
    }
 
    if (e.CellElement.ColumnInfo.Name == "Priority")
    {
        // your formatting logic here
    }
}

I hope it helps you to accomplish your requirements.

Kind regards,
Alexander
the Telerik team
Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
Tags
GridView
Asked by
Khizar Khan
Top achievements
Rank 1
Answers by
Alexander
Telerik team
Share this question
or