Showing posts with label Telerik. Show all posts
Showing posts with label Telerik. Show all posts

Thursday, December 30, 2010

Silverlight Programming: RadComboBox Virtualization

Telerik RadControls' API gives you the ability to configure the RadComboBox to support virtualization, which reduces the memory footprint of the application and speeds up the loading time thus enhancing additionally the UI performance. Some times its required to load thousands of items in a RadComboBox. By default the control creates RadComboBoxItem containers for each data item, so it might take some time to open the drop-down. To resolve the problem you just have to change the RadComboBox's ItemsPanel with VirtualizingStackPanel:

Here is the snippet of code block

<telerik:RadComboBox HorizontalAlignment="Left" TextSearchMode="StartsWith"
IsFilteringEnabled="True" OpenDropDownOnFocus="True" Width="200" 
IsEditable="True" IsDropDownOpen="False" Name="AccountDropDownList" 
SelectedValuePath="customer_number" DisplayMemberPath="customer_name" >
  <telerik:RadComboBox.ItemsPanel>
       <ItemsPanelTemplate>
        <VirtualizingStackPanel />
      </ItemsPanelTemplate>
  </telerik:RadComboBox.ItemsPanel>
</telerik:RadComboBox>

Hope this is useful Nerd smile

Thursday, December 09, 2010

Exporting to Excel from Telerik Gridview in Silverlight

Here is the sample code for exporting Telerik Gridview in silverlight.

private void btnExportOrders_Click(object sender, RoutedEventArgs e)
{
   // Checking whether grid has rows
    if (OrderGrid.Items.Count > 0)
    {
          string extension = "xls";
          string selectedItem = "Excel";
          ExportFormat format = ExportFormat.ExcelML;
          SaveFileDialog dialog = new SaveFileDialog();
          dialog.DefaultExt = extension;
          dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, selectedItem);
          dialog.FilterIndex = 1;
          if (dialog.ShowDialog() == true)
          {
              using (Stream stream = dialog.OpenFile())
              {
                GridViewExportOptions options = new GridViewExportOptions();
                options.Format = format;
                options.ShowColumnHeaders = true;
                options.Encoding = Encoding.UTF8;
                options.ShowColumnHeaders = true;
               // name of the grid which you want export      
                OrderGrid.Export(stream, options);
               }
           }
     }
     else
      {
         // User friendly Message
          MessageBox.Show("There are no records to export.", "Export Warning!", MessageBoxButton.OK);
      }
 }

This export code will also support different formats like CSV,Text and HTML. All you need to change is the enum type for ExportFormat to above formats and the extension. Its easy and simple. Good luck Winking smile

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.