Hi,
I am trying to set the color of each cell in the grid based on the value of the label contained inside of the item template.
Here is my grid markup:
In the code behind I am trying to do the following, though not much luck finding the right control:
I am trying to set the color of each cell in the grid based on the value of the label contained inside of the item template.
Here is my grid markup:
<
telerik:RadGrid
ID
=
"rgSchedule"
runat
=
"server"
AutoGenerateColumns
=
"False"
CellSpacing
=
"0"
DataSourceID
=
"sqlWebinarSchedule"
GridLines
=
"None"
OnItemDataBound
=
"rgSchedule_ItemDataBound"
>
<
MasterTableView
DataSourceID
=
"sqlWebinarSchedule"
>
<
CommandItemSettings
ExportToPdfText
=
"Export to PDF"
></
CommandItemSettings
>
<
RowIndicatorColumn
Visible
=
"True"
FilterControlAltText
=
"Filter RowIndicator column"
>
</
RowIndicatorColumn
>
<
ExpandCollapseColumn
Visible
=
"True"
FilterControlAltText
=
"Filter ExpandColumn column"
>
</
ExpandCollapseColumn
>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"WeekRange"
FilterControlAltText
=
"Filter WeekRange column"
ReadOnly
=
"True"
SortExpression
=
"WeekRange"
UniqueName
=
"WeekRange"
>
</
telerik:GridBoundColumn
>
<
telerik:GridTemplateColumn
DataField
=
"MondayDay"
DataType
=
"System.Int32"
FilterControlAltText
=
"Filter MondayDay column"
HeaderText
=
"Monday"
SortExpression
=
"MondayDay"
UniqueName
=
"Monday"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"MondayDayLabel"
runat
=
"server"
Text='<%# Eval("MondayDay") %>'></
asp:Label
>
<
asp:Label
ID
=
"MondayColor"
runat
=
"server"
Text='<%# Eval("Monday") %>'></
asp:Label
>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridTemplateColumn
DataField
=
"TuesdayDay"
DataType
=
"System.Int32"
FilterControlAltText
=
"Filter TuesdayDay column"
HeaderText
=
"Tuesday"
SortExpression
=
"TuesdayDay"
UniqueName
=
"Tuesday"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"TuesdayDayLabel"
runat
=
"server"
Text='<%# Eval("TuesdayDay") %>'></
asp:Label
>
<
asp:Label
ID
=
"TuesdayColor"
runat
=
"server"
Text='<%# Eval("Tuesday") %>'></
asp:Label
>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridTemplateColumn
DataField
=
"WednesdayDay"
DataType
=
"System.Int32"
FilterControlAltText
=
"Filter WednesdayDay column"
HeaderText
=
"Wednesday"
SortExpression
=
"WednesdayDay"
UniqueName
=
"Wednesday"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"WednesdayDayLabel"
runat
=
"server"
Text='<%# Eval("WednesdayDay") %>'></
asp:Label
>
<
asp:Label
ID
=
"WednesdayColor"
runat
=
"server"
Text='<%# Eval("Wednesday") %>'></
asp:Label
>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridTemplateColumn
DataField
=
"ThursdayDay"
DataType
=
"System.Int32"
FilterControlAltText
=
"Filter ThursdayDay column"
HeaderText
=
"Thursday"
SortExpression
=
"ThursdayDay"
UniqueName
=
"Thursday"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"ThursdayDayLabel"
runat
=
"server"
Text='<%# Eval("ThursdayDay") %>'></
asp:Label
>
<
asp:Label
ID
=
"ThursdayColor"
runat
=
"server"
Text='<%# Eval("Thursday") %>'></
asp:Label
>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridTemplateColumn
DataField
=
"FridayDay"
DataType
=
"System.Int32"
FilterControlAltText
=
"Filter FridayDay column"
HeaderText
=
"Friday"
SortExpression
=
"FridayDay"
UniqueName
=
"Friday"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"FridayDayLabel"
runat
=
"server"
Text='<%# Eval("FridayDay") %>'></
asp:Label
>
<
asp:Label
ID
=
"FridayColor"
runat
=
"server"
Text='<%# Eval("Friday") %>'></
asp:Label
>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridTemplateColumn
DataField
=
"SaturdayDay"
DataType
=
"System.Int32"
FilterControlAltText
=
"Filter SaturdayDay column"
HeaderText
=
"Saturday"
SortExpression
=
"SaturdayDay"
UniqueName
=
"Saturday"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"SaturdayDayLabel"
runat
=
"server"
Text='<%# Eval("SaturdayDay") %>'></
asp:Label
>
<
asp:Label
ID
=
"SaturdayColor"
runat
=
"server"
Text='<%# Eval("Saturday") %>'></
asp:Label
>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridTemplateColumn
DataField
=
"SundayDay"
DataType
=
"System.Int32"
FilterControlAltText
=
"Filter SundayDay column"
HeaderText
=
"Sunday"
SortExpression
=
"SundayDay"
UniqueName
=
"Sunday"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"SundayDayLabel"
runat
=
"server"
Text='<%# Eval("SundayDay") %>'></
asp:Label
>
<
asp:Label
ID
=
"SundayColor"
runat
=
"server"
Text='<%# Eval("Sunday") %>'></
asp:Label
>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
</
Columns
>
<
GroupByExpressions
>
<
telerik:GridGroupByExpression
>
<
SelectFields
>
<
telerik:GridGroupByField
FieldAlias
=
"MonthYear"
FieldName
=
"MonthYear"
FormatString
=
""
HeaderText
=
"Month"
/>
</
SelectFields
>
<
GroupByFields
>
<
telerik:GridGroupByField
FieldAlias
=
"MonthStart"
FieldName
=
"MonthStart"
FormatString
=
""
HeaderText
=
""
/>
</
GroupByFields
>
</
telerik:GridGroupByExpression
>
</
GroupByExpressions
>
<
EditFormSettings
>
<
EditColumn
FilterControlAltText
=
"Filter EditCommandColumn column"
></
EditColumn
>
</
EditFormSettings
>
</
MasterTableView
>
<
FilterMenu
EnableImageSprites
=
"False"
></
FilterMenu
>
</
telerik:RadGrid
>
In the code behind I am trying to do the following, though not much luck finding the right control:
protected
void
rgSchedule_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem item = e.Item
as
GridDataItem;
foreach
(Control ctrl
in
item.Controls)
{
if
(ctrl
is
Label)
{
Label lbl = (Label)ctrl;
switch
(lbl.Text)
{
case
"Red"
:
item.BackColor = System.Drawing.Color.Red;
break
;
case
"White"
:
item.BackColor = System.Drawing.Color.White;
break
;
case
"Green"
:
item.BackColor = System.Drawing.Color.Green;
break
;
}
rgSchedule.Rebind();
}
}
}
}