Archive for the ‘DevExpress’ Category

Perfoming calulations and edits on a partial read-only DXGrid

Sunday, October 16th, 2011

Today’s writeup is was triggered by a question from a reader. While its something that probaly everyone archieved at some point using the standard Microsoft grid control, the sheer amount of features avaiable on the Devexpress DXGrid make things sometimes less obvious for those starting out with the the suite.

So here is a complete working sample, that does the following:

- Read the starter data from a table, and present it in a grid control
- Allow edits to only one column
- We want to highlight the editable column with a color to draw the user’s attention to it.
- We want the pricing and line total column to use a currency layout
- We want the pricing and line total column to be right aligned
- Perform a calculation everytime an edit has been made
(more…)

Populating a Treelist control from code or data, and reacting to click events

Sunday, June 12th, 2011

These samples will show the basic handling of the treelist control, to create a structure and react to clicks by the user. Used in this example is the treelist control from the DevExpress suit, but you can apply the same technique to basicaly any treelist control with minimal changes.

The first example shows how to manualy build a tree from code, and then react to clicks. The second example archives the exact same tree, but retrieves it’s data from a simple sql server table, making the content of the tree list much more dynamic. (more…)

Controling the tray to use for printing with Devexpress XtraReports

Monday, May 16th, 2011

This sample shows how to enumerate and select a specific tray to use for printing.

The code works bij intercepting the oReport.PrintingSystem.StartPrint event, by adding a handler and sending it to a routine which enumerates the trays of the printer.

(more…)

DevExpress – Coloring individual cells or rows in a Gridcontrol based on a cell value

Wednesday, April 20th, 2011

DevExpress – Coloring individual cells or rows in a Gridcontrol based on a cell value. This sample hooks into the RowCellStyle event of the Gridview, and evaluates the value of the cell. Depending on the outcome, a background color is assigned to the cell. (more…)

Programmatically formatting grid view columns MS vs. DevExpress

Friday, February 11th, 2011

DevExpress


            ' --- Apply currency format
            GridView7.Columns("price").DisplayFormat.FormatString = "c"
            GridView7.Columns("price").DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom
            ' //

            ' --- Right align a column
            GridViewArtikelInslag.Columns("datum").AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far
            ' //

            ' --- Auto fit sizes to content
            GridView7.OptionsView.ColumnAutoWidth = False
            GridView7.BestFitColumns()
            ' //

Default .NET Control


            ' --- Activate auto sizing
            DataGridViewA.AutoSizeColumnsMode = DataGridViewAutoSizeColumnMode.Fill
            ' //

            '--- Apply currency format
            DataGridViewA.Columns("price").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            ' //

            ' --- Set fill weight
            DataGridViewA.Columns("price").FillWeight = 300
            ' //

DevExpress – sizing the columns of a gridcontrol to the content

Tuesday, January 18th, 2011

By default the DevExpress grid control will force all columsn to fit in the default view. These property’s can be used to change that behaviour and have teh columns size to the content of the cells and a scrollbar to appear.

        ' --- A blank grid control is present on the form, which will supply the
        '     gridview1 object by default.

        ' --- By default, ALL columns will be forced into the avaiable width
        GridView1.OptionsView.ColumnAutoWidth = False

        ' --- Size the columns to the content
        GridView1.BestFitColumns()

The following code snippet applies the technique to a form which contains a gridcontrol with no bindinge made at design time. At runtime, a qeury is run against an ODBC database, and the result of that query is visualized in the grid.

Code to run the query and bind the results to the grid:

    Private Sub RunQuery()
        Using oCon As New OdbcConnection(My.Settings.ODBCLink)
            oCon.Open()
            Dim sQuery As String = TextEdit1.Text
            Using oDa As New OdbcDataAdapter(sQuery, oCon)
                Using oDs As New DataSet

                    oDa.Fill(oDs, "resultset")
                    GridControl1.DataSource = oDs
                    GridControl1.DataMember = "resultset"

                End Using
            End Using
        End Using
    End Sub

And apply the formatting so we can see all resulting data

        GridView1.OptionsView.ColumnAutoWidth = False
        GridView1.BestFitColumns()

Creating a context (right-click) menu on a datagridview

Sunday, January 9th, 2011

In addition to the method below, you can also use the standard context menu provided by Visual Studio. The downside of the standard control is that it will not select the cell oyou are on when used with a datagrid. To get that behaviour you can trap the MouseDown event on the datagrid which is executed before the context menu control. So you could simply bind a standard context menu to the datagrid and this as handler:

    Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown

    Try
        DataGridView1.CurrentCell = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)
    Catch ex As ArgumentException
        ' --- The user clicked outside a valid cell, we cant select
        Beep()
    End Try

End Sub

If you are using a DevExpress gridcontrol, the code changes slightly, but still comes down to the same. After binding the standard context menu control to the gridcontrol, you can reference the currently selected row and retrive the value from a cell in it:

    ''' <summary>
    ''' this is the handler for the context menu's option
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub BewerkenToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles BewerkenToolStripMenuItem.Click

        ' --- Retrieve the value from the ID column from the currently selected row
        Dim row As System.Data.DataRow = GridView1.GetDataRow(GridView1.FocusedRowHandle)
        MsgBox(row.Item("id"))

    End Sub

The method below is an alternative method which has the same behaviour, but does not use the context menu control supplied by Visual Studio.
Additionaly, the method below dows not show the menu as a response to rigth clicks when the mouse is over a blank are of the grid.

Private Sub DataGridView1_CellMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseUp

            If e.Button = Windows.Forms.MouseButtons.Right Then

              If e.ColumnIndex = -1 = False And e.RowIndex = -1 = False Then
                    ' Valid cell, Select it
                    Me.DataGridView1.ClearSelection()
                    Me.DataGridView1.CurrentCell = Me.DataGridView1.Item(e.ColumnIndex, e.RowIndex)

                    ' Popup the context menu
                    Dim mygrid As DataGridView = sender
                    Dim mymenu As ContextMenuStrip = New ContextMenuStrip
                    mymenu.Items.Add("Option 1", Nothing, AddressOf Option1Call)
                    mymenu.Items.Add("Option 2", Nothing, AddressOf Option2Call)
                    Dim mypoint As Point = mygrid.PointToClient(Control.MousePosition)
                    mymenu.Show(DataGridView1, mypoint.X, mypoint.Y)
                End If
            End If

        End Sub

        Public Sub Option1Call()
            Dim n_current_row As Integer
            Dim n_clicked_id As Integer
            Dim d_row As DataGridViewRow
            ' get the selected gridrow
            n_current_row = Me.DataGridView1.CurrentCell.RowIndex
            If n_current_row < 0 Then Exit Sub ' no data on this gridline
            d_row = DataGridView1.Rows(n_current_row)
            ' get the id value of the database record (assuming column 0 contains the id)
            n_clicked_id = d_row.Cells(0).Value
            MsgBox("Clicked database id: " &amp;amp;amp; n_clicked_id)
        End Sub

        Public Sub Option2Call()
            MsgBox("Option 2 clicked")
        End Sub

Binding a LINQ Query to a grid control

Sunday, January 9th, 2011
        Using oCon As New SqlConnection(My.Settings.ConString)
            Using oDc As New SomeDataContext(oCon)

                Dim oQuery = From pr In oDc.SomeTable _
                             Order By pr.field1, pr.field2 _
                             Select pr.field1, pr.field2, pr.field3, pr.field4

                GridControl1.DataSource = oQuery.ToList

                GridView1.OptionsBehavior.Editable = False

            End Using
        End Using