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

Change color of Cell in Gridview

3 Answers 125 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mohammed
Top achievements
Rank 1
Mohammed asked on 08 Apr 2011, 02:20 AM
Hi everyone,

I'm now to ASP.net and programming work i use the RADGridView in my website.
the question is how I can change the color of the cell based on a specific conditions that my column is a datetime. I want if the due date is pass the color should be RED.


Thanks for your help in advance

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 08 Apr 2011, 06:07 AM
Hi

protected void Data_OnItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
if (Convert.ToDateTime(item.Cells["ColumnUniqueName"].Text) < DateTime.Today )
{
item.Cells["ColumnUniqueName"].ForeColor = System.Drawing.Color.Red;
}
}
}


http://www.telerik.com/help/aspnet-ajax/grdaccessingcellsandrows.html

Thanks,
Jayesh Goyani
0
Mohammed
Top achievements
Rank 1
answered on 08 Apr 2011, 05:44 PM
thanks a lot for your great help  Jayesh
I just convert your code to VB.NET and modifies a small issue

my code is the following
    Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound

        If (TypeOf e.Item Is GridDataItem) Then
            Dim dataItem As GridDataItem = CType(e.Item, GridDataItem)
            Dim FromDate As Date
            '  Dim todate As Date = dataItem("DueDate").Text
            FromDate = (DateTime.ParseExact(dataItem("DueDate").Text, "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat))
            Dim CellValue As DateTime = Convert.ToDateTime(FromDate)
            If DateDiff(DateInterval.Day, CellValue, Today.Date) > 1 Then         
                dataItem.Cells(6).BackColor = System.Drawing.Color.Red
            ElseIf DateDiff(DateInterval.Day, CellValue, Today.Date) > 3 Then
          
                dataItem.Cells(6).BackColor = System.Drawing.Color.Orange
             

            End If
        End If

    End Sub

0
Jayesh Goyani
Top achievements
Rank 2
answered on 11 Apr 2011, 01:26 PM
hi Mohammed,

try to convert this code using below website.
http://converter.telerik.com/

if u still get error let me know error detail.

Thanx,
Jayesh Goyani
Tags
Grid
Asked by
Mohammed
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Mohammed
Top achievements
Rank 1
Share this question
or