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

Set the cell color based on the template label value

3 Answers 434 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Yuriy
Top achievements
Rank 1
Yuriy asked on 03 Oct 2012, 01:45 AM
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:
  <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();
                   }
               }
           }
       }

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 03 Oct 2012, 04:23 AM
Hi,

Try accessing the label as shown below.
C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
 if (e.Item is GridDataItem)
 {
    GridDataItem item = (GridDataItem)e.Item;
    Label lbl = (Label)item.FindControl("MOndayDayLabel");
    if (lbl.Text == "Monday")
    {
       item.BackColor = System.Drawing.Color.Red;
    }
  }
}

Thanks,
Shinu.
0
Yuriy
Top achievements
Rank 1
answered on 05 Oct 2012, 01:56 AM
Hi Shinu,

I thought of the simpler solution for this. Anyways, this is what I did:
protected void rgSchedule_ItemDataBound(object sender, GridItemEventArgs e)
       {
          
 
           if (e.Item is GridDataItem)
           {
               GridDataItem item = e.Item as GridDataItem;
               string[] Days = new string[7] { "MondayColor", "TuesdayColor", "WednesdayColor", "ThursdayColor", "FridayColor", "SaturdayColor", "SundayColor" };
 
               foreach (string day in Days)
               {
                   Label lbl = item.FindControl(day) as Label;
                       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();
            }
       }
However the color of cells does not change. When I uncomment rgSchedule.Rebind() i get the exception:
Insufficient stack to continue executing the program safely. This can happen from having too many functions on the call stack or function on the stack using too much stack space.

Any suggestions?


Thanks,
Yuriy

0
Shinu
Top achievements
Rank 2
answered on 05 Oct 2012, 07:58 AM
Hi,

I couldn't replicate any exception. Please try the following code snippet to give color for the cell.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;
        string[] Days = new string[4]{ "MondayColor", "TuesdayColor", "WednesdayColor", "ThursdayColor", "FridayColor", "SaturdayColor", "SundayColor" };     
            foreach (string day in Days)
            {
                Label lbl = item.FindControl(day) as Label;
                TableCell cell = (TableCell)lbl.Parent;
                switch (lbl.Text)
                {
                    case "red":
                       cell.BackColor = System.Drawing.Color.Red;
                        break;
 
                    case "white":
                        cell.BackColor = System.Drawing.Color.White;
                        break;
 
                    case "green":
                       cell.BackColor = System.Drawing.Color.Green;
                        break;
                 }
 
            }    
    }
}

Thanks,
Shinu.
Tags
Grid
Asked by
Yuriy
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Yuriy
Top achievements
Rank 1
Share this question
or