Thursday, May 20, 2010

HEX to Color in Silverlight GridView

Recently i need to use colors in my grid cells, for that i have written code in RadGridView_RowLoaded to set the colors. But i had trouble setting color as background to cells. I could able to achieve this way.

 // Setting backgrounds to cells 
        private void RadGridView1_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
        {
            string strPirority = "";
            string strStage = "";
            if (e.Row.Item != null)
            {
                strPirority = ((TelerikOrderSilverlight.ServiceReference1.v_order_mgmt)(((Telerik.Windows.Controls.RadRowItem)(((Telerik.Windows.Controls.GridView.GridViewRowItemEventArgs)(e)).Row)).Item)).priority.ToString();
                if (((TelerikOrderSilverlight.ServiceReference1.v_order_mgmt)(((Telerik.Windows.Controls.RadRowItem)(((Telerik.Windows.Controls.GridView.GridViewRowItemEventArgs)(e)).Row)).Item)).stage != null)
                {
                    strStage = ((TelerikOrderSilverlight.ServiceReference1.v_order_mgmt)(((Telerik.Windows.Controls.RadRowItem)(((Telerik.Windows.Controls.GridView.GridViewRowItemEventArgs)(e)).Row)).Item)).stage.ToString();
                }               
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    if (e.Row.Cells[i].Column.UniqueName == "priority")
                    {
                        if (strPirority == "Critical")
                        {
                            // Can set this way using solid
                            e.Row.Cells[i].Background = new System.Windows.Media.SolidColorBrush(Colors.Red);
                        }
                    }
                    if (e.Row.Cells[i].Column.UniqueName == "MileStone")
                    {
                        if (strStage.Length > 0)
                        {
                            // You can set this way using Hexa codes.
                            if (strStage.Substring(0, 4) == "Bill")
                            {
                                e.Row.Cells[i].Background = new System.Windows.Media.SolidColorBrush(GetColorFromHexa("#FF000000"));
                            }
                        }
                    }
                }
            }
        }
        private Color GetColorFromHexa(string hexaColor)
        {
            return Color.FromArgb(
                    Convert.ToByte(hexaColor.Substring(1, 2), 16),
                    Convert.ToByte(hexaColor.Substring(3, 2), 16),
                    Convert.ToByte(hexaColor.Substring(5, 2), 16),
                    Convert.ToByte(hexaColor.Substring(7, 2), 16));
        }


Here is the list of all predefined SolidColorBrush objects.

No comments:

Post a Comment