Thursday, April 14, 2016

Working Events of Telerik Grid View Windows Controls

Here is some events where you can do some operations while working with telerik gridview.

(1) Cell double click :- You can do some functionality while clicking over cell of the grid item, it could be anything like opening a another form in edit mode or anything.
Private Sub grid_CellDoubleClick(sender As Object, e As EventArgs) Handles grid.CellDoubleClick
'Your code goes here        
End Sub

(2) Row Formatting :- When you want to do some operations on your grid items this event comes to you. for me, on some operation i need to show items color to red, i checked its column value and if it meets requirement make it red else reset value of the.
Private Sub rgdAvailResult_RowFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.RowFormattingEventArgs) Handles rgdAvailResult.RowFormatting
    If e.RowElement.RowInfo.Cells("column1").Value Then
       e.RowElement.ForeColor = System.Drawing.Color.Red
    Else
       e.RowElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local)
    End If
End Sub

(3) View Row Formatting :- for rows related general event to work with different items of the grid. For me, to show filter bar in different color format, i use this event. So, if the item is filterrow then change is color else reset value to original.
Private Sub grid_ViewRowFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.RowFormattingEventArgs)
        If TypeOf e.RowElement Is GridFilterRowElement Then
            e.RowElement.BackColor = Color.NavajoWhite
        Else
            e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local)
        End If
    End Sub

So, these are some events which are i found for my resolution. 

Wednesday, April 13, 2016

Export Telerik Windows GridView Control Data to Excel File

Here is some code snippet which will generate excel file of the telerik windows grid view. I assuming that you already bind data to your telerik grid on your windows application form and performing export to excel on any event.

- Declare new save file dialog in which you will enter your file name
Dim saveFileDialog As New System.Windows.Forms.SaveFileDialog()
- Below code snippet filters your file to be saved for xls only
saveFileDialog.Filter = "Excel (*.xls)|*.xls"
If saveFileDialog.ShowDialog() <> DialogResult.OK Then
    Return
End If
- Below code snippet checks whether file name has been entered or not
If saveFileDialog.FileName.Equals([String].Empty) Then
    RadMessageBox.SetThemeName(MyBase.ThemeName)
    RadMessageBox.Show("Please enter a file name.")
    Return
End If
- Below code snippet acceps name you entered and some default entries to generate your file
Dim fileName As String = saveFileDialog.FileName
Dim excelExporter As New ExportToExcelML("GridName")
excelExporter.SummariesExportOption = SummariesOption.ExportAll
excelExporter.SheetMaxRows = ExcelMaxRows._1048576
excelExporter.RunExport(fileName)
RadMessageBox.SetThemeName(MyBase.ThemeName)
- Below code snippet asks dialog to open saved file 
Dim dr As DialogResult = RadMessageBox.Show("The data in the grid was exported successfully. Do you want to open the file?", "Export to Excel", MessageBoxButtons.YesNo, RadMessageIcon.Question)
If dr = DialogResult.Yes Then
    System.Diagnostics.Process.Start(fileName)
End If

This way you can export your telerik windows control grid view data to excel file in default format.

Tuesday, April 12, 2016

Add an Item to Context Menu of Telerik Windows Grid View

Here is some code to work with Context Menu of Grid View of Telerik Windows Controls for Windows Forms

How to add an item to context menu of Grid View : To add custom item to context menu of the telerik grid view follow the steps below. For this code, I assume you are using Telerik Grid View for your Windows Forms and you have performed all necessary steps to bind your grid.

- Add an event handler 
AddHandler radGridView1.ContextMenuOpening, AddressOf radGridView1_ContextMenuOpening1

- Create contet menu event
Private Sub radGridView1_ContextMenuOpening1(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.ContextMenuOpeningEventArgs)
        Dim menuItemExportToSpreadsheet As RadMenuItem = New RadMenuItem()
        menuItemExportToSpreadsheet.Text = "Export to Spreadsheet"
        AddHandler menuItemExportToSpreadsheet.Click, AddressOf ExportToSpreadsheet

        Dim separator As RadMenuSeparatorItem = New RadMenuSeparatorItem()
        e.ContextMenu.Items.Add(separator)
        e.ContextMenu.Items.Add(menuItemExportToSpreadsheet)
    End Sub

- Create click event of the newly added context menu item
Private Sub ExportToSpreadsheet(ByVal sender As Object, ByVal e As System.EventArgs)
'Your code goes here
End Sub

So, after doing this, each time you are on your grid view and you are opening your context menu you will see a new item get added to your context menu named "Expor to Spreadsheet", and on click of this you can perform logic written under its handler. 

This way you can add new menu item under your Telerik Grid Context Menu.