HOME PAGE | DOWNLOAD | TUTORIALS | XtraReports
Devexpress
Showing posts with label Grids. Show all posts
Showing posts with label Grids. Show all posts

Sunday, July 22, 2012

How to: Validate Modified Rows

Assume that a Grid View contains two columns: "Units In Stock" and "Units On Order". A value in the first column must be more than the value of the second one. So we need to perform validation of a row when it is about to be saved to the data source. For this purpose, the ColumnView.ValidateRow event is handled.
If row fails validation, we set errors for the columns with coresponding descriptions using the ColumnView.SetColumnError method. The descriptions will be displayed when hovering over error icons.
The ColumnView.InvalidRowException event is handled in order to suppress displaying the default error message box.
The following screenshot shows a Grid View after a row fails validation.

C#
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Controls;

private void gridView1_ValidateRow(object sender,
DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e) {
    GridView view = sender as GridView;
    GridColumn inStockCol = view.Columns["UnitsInStock"];
    GridColumn onOrderCol = view.Columns["UnitsOnOrder"];
    //Get the value of the first column
    Int16 inSt = (Int16)view.GetRowCellValue(e.RowHandle, inStockCol);
    //Get the value of the second column
    Int16 onOrd = (Int16)view.GetRowCellValue(e.RowHandle, onOrderCol);
    //Validity criterion
    if (inSt < onOrd) {
        e.Valid = false;
        //Set errors with specific descriptions for the columns
        view.SetColumnError(inStockCol, "The value must be greater than Units On Order");
        view.SetColumnError(onOrderCol, "The value must be less than Units In Stock");
    }
}

private void gridView1_InvalidRowException(object sender,
DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e) {
    //Suppress displaying the error message box
    e.ExceptionMode = ExceptionMode.NoAction;
}

VB
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraEditors.Controls
 
Private Sub GridView1_ValidateRow(ByVal sender As Object, _
ByVal e As DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs) _
Handles GridView1.ValidateRow
    Dim view As GridView = CType(sender, GridView)
    Dim inStockCol As GridColumn = View.Columns("UnitsInStock")
    Dim onOrderCol As GridColumn = View.Columns("UnitsOnOrder")
    'Get the value of the first column
    Dim inSt As Int16 = CType(view.GetRowCellValue(e.RowHandle, UnitsInStock), Int16)
    'Get the value of the second column
    Dim onOrd As Int16 = CType(view.GetRowCellValue(e.RowHandle, UnitsOnOrder), Int16)
    'Validity criterion
    If inSt < onOrd Then
        e.Valid = False
        'Set errors with specific descriptions for the columns
        View.SetColumnError(inStockCol, "The value must be greater than Units On Order")
        View.SetColumnError(onOrderCol, "The value must be less than Units In Stock")
    End If
End Sub
 
Private Sub GridView1_InvalidRowException(ByVal sender As Object, _
ByVal e As DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs) _
Handles GridView1.InvalidRowException
    'Suppress displaying the error message box
    e.ExceptionMode = ExceptionMode.NoAction
End Sub
 

Enhanced by Zemanta

Saturday, July 14, 2012

XtraGrid - Display Master-Detail Data

In this lesson, I’ll demonstrate how to use a Grid Control to present data in a Master-Detail format.
1.       First, I’m going to add a Detail Data Table to the Customers DataSet.
2.       To do this, I run the DataSet Configuration Wizard.
3.       Now, I’m going to select the Childs table for the Customers.
4.       Let it be the Orders Table.
5.       I choose the fields that I want to be retrieved,  . . . and click Finish.
6.       Notice that the detail buttons which I use to expand detailed views, are now displayed within the grid rows.
7.       This indicates that our Grid Control is ready to display Master-Detail data.
8.       By default, details will be represented by grid views.
9.       Now, I’ll show you how to change the view used to display detail data.
10.   For this purpose, first click the “Retrieve Details” button.
11.   The Grid Control creates a Master-Detail tree that corresponds to the structure of your data source.
12.   After the detail level has been created, we can associate a view with it.
13.   Let’s use a newly created Card View.
14.   After these steps are complete, I rebuild the solution.
15.   Finally, I need to supply the data to be displayed by the detail views.
16.   I drop the “Orders Table Adapter” onto the form.
17.   Then I can double-click the form, and write code within the form’s “Load” Event Handler to fill the dataset with the data provided by the “Orders Table Adapter”.
CODE:
ordersTableAdapter1.Fill(dsCustomers.Orders);
18.   And that’s all . . . I run the application to see the results.
19.   Expand the first master row.
20.   You can see that the detail data is now represented as cards.
21.   I can maximize the detail, so that it occupies the entire grid control’s window.
22.   Then, I can switch back to the main view.
The example shown in this lesson is the simplest Master-Detail structure you can display using the XtraGrid. You can have any number of nesting levels and any number of details at each level.
For additional information, please refer to the XtraGrid’s documentation.

Enhanced by Zemanta

XtraGrid - Band and Column Customization

This lesson demonstrates the basics of working with banded Grid Views. I’ll show you to add and delete bands, customize band settings, and change the layout of bands and columns.
1.       First, let’s change the view type from GridView to “Advanced Banded Grid View”.
2.       You’ll see that all the columns are now under the default band.
3.       Let’s add one more band to break the columns up into two logical parts.
4.       For this purpose, we need to run the designer.
5.       You can add bands and specify their location at the same time by pressing the “Add New Band” button and dragging the mouse pointer to the desired band position.
6.       After you’ve added the band, you can click its header to access the band settings.
7.       In this example, we can use this feature to change the band caption.
8.       To rearrange columns between bands, I simply drag the desired columns and drop them where they are needed.
9.       Now I’m done with the layout customizations, so I’ll close the designer.
10.   The columns and bands layout can also be customized right on the form.
11.   Let’s drag columns one under another, so that the corresponding data cells are arranged to two rows.
12.   Let’s now make the grid a little bit more readable, by stretching the columns to fit the entire view.
13.   The Column’s “Auto Width View” option needs to be set to true for this particular purpose.
14.   Now we need to make the column headers in the second band, occupy two rows.
15.   To do this, we simply click the column headers (we could use the “Control” or “Shift” key to select multiple columns), and once selected, I set their “Auto Fill Down” property to true to make them stretch down automatically.
16.   Finally, let’s change the caption of the “Customer ID” column using the same approach.
17.   Now I’m done with banded column customizations, so let’s run the application to see the result.

Enhanced by Zemanta