• Using Custom Edit Events [Janus GridEX WinForms Control v3.5 for .NET]

    Steps to reproduce this sample:



    1) Create a new "Windows Application" Project.



    2) Add Customers Table from JSNorthwind database as a Data Source for the application.



    3) Add GridEX Control to a tab in the Toolbox window by right clicking in the Toolbox window and choosing "Customize Toolbox..." menu. When the dialog appears, select ".Net Framework Components" tab, check "GridEX" control in the list and click OK.
    Note: If GridEX control doesn't appear as an option in the list, click the Browse button and open Janus.Windows.GridEX.dll.



    4) Drag a GridEX control from the toolbox into the Form designer.



    5) From the "Data Sources" window in the Application, drag Customers and drop it into the new GridEX control. JSNorthwindDataSet, CustomersTableAdapter and CustomersBindingSource components will be created.



    5) Right click in the GridEX control and select "Retrieve Structure" menu. This action will force the control to read the DataSource structure and create the tables and columns needed to show all the fields in the tables.



    6) Right click in the GridEX control again and select "GridEX Designer" menu. In the GridEX designer dialog, select "Columns" under GridEX/RootTable node.



    7) In the right pane, select "CustomerID" column and change the EditType property to Custom. Setting this property, you will be able to get the InitCustomEdit event every time the user tries to edit a cell in this column.



    8) Drop a TextBox control into the Form and change its name to txtCustom. This text box will be the one used as the edit window in the CustomerID column.




    9) In the InitCustomEdit event of the GridEX control, set the Text property of the TextBox acting as the custom edit control and set the EditControl property of the InitCustomEditEventArgs parameter to the txtCustom control.



    In VB .NET



    'For the sample, we will use BackColor Yellow in the TextBox

    'if the cell is in a new row

    If e.Row.RowType = RowType.NewRecord Then

    txtCustom.BackColor = Color.Yellow

    Else

    txtCustom.BackColor = e.FormatStyle.BackColor

    End If



    'When the user start edition by pressing a key,

    'the EditChar property holds the char that

    'started the edition. If edition was started

    'because the user clicked in the cell the

    'EditChar returns (char)0

    If Char.IsLetterOrDigit(e.EditChar) Then

    txtCustom.Text = e.EditChar.ToString()

    txtCustom.SelectionStart = txtCustom.Text.Length

    Else

    If e.Value Is Nothing Then

    txtCustom.Text = ""

    Else

    txtCustom.Text = e.Value.ToString()

    End If

    txtCustom.SelectionLength = txtCustom.Text.Length

    End If

    'Set the EditControl property to let the GridEX control

    'know which control to position in the cell.

    e.EditControl = txtCustom



    In C# .NET



    //For the sample, we will allow to edit

    //the CustomerID field only in new rows.

    //So, we set the ReadOnly property to false

    //if rows with RowType set to Record.

    if(e.Row.RowType==RowType.NewRecord)

    {

    txtCustom.ReadOnly=false;

    }

    else

    {

    txtCustom.ReadOnly = true;

    }

    //When the user start edition by pressing a key,

    //the EditChar property holds the char that started

    //the edition. If edition was started because the

    //user clicked in the cell the EditChar

    //returns (char)0

    if(e.EditChar!=(char)0 && !txtCustom.ReadOnly)

    {

    txtCustom.Text = e.EditChar.ToString();

    txtCustom.SelectionStart = txtCustom.Text.Length;

    }

    else

    {

    if(e.Value==null)

    {

    txtCustom.Text = "";

    }

    else

    {

    txtCustom.Text = e.Value.ToString();

    }

    txtCustom.SelectionLength = txtCustom.Text.Length;

    }

    //Set the EditControl property to let the GridEX control

    //know which control to position in the cell.

    e.EditControl = txtCustom;



    10) In the EndCustomEdit event of the GridEX control, compare value of the value in the TextBox to the original value in the cell. If the value has changed, set the Value property of the EndCustomEditEventArgs parameter to the new value and set the DataChanged property to true to inform the GridEX control that the cell must be updated in the data source.



    In VB .NET

    If Not e.CancelUpdate Then

    If e.Value <> txtCustom.Text Then

    e.Value = txtCustom.Text

    End If

    End If



    In C# .NET



    //Compare the original value with

    //the value in the control.

    if(txtCustom.Text.CompareTo(e.Value)!=0)

    {
    //If the value is different,

    //set the DataChanged property to true

    //to indicate the control that it has

    //to update the cell value.

    e.DataChanged = true;

    e.Value = txtCustom.Text;
    }
    11) Press F5 and run the project.

    Source Of Information : Janus v3.5 Help Files for VS 2008


0 comments:

Leave a Reply