How to access properties of the active cell in the DataGrid control by using Visual C++ 2005 or Visual C++ .NET (816193)



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft .NET Framework 1.1

For a Microsoft Visual Basic .NET version of this article, see 321296.
For a Microsoft Visual C# .NET version of this article, see 321293.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
  • System::ComponentModel
  • System::Collections
  • System::Windows::Forms
  • System::Data
  • System::Drawing

IN THIS TASK

INTRODUCTION

This step-by-step article describes how to obtain a reference to the active cell in the DataGrid control. This article also describes how to access different properties of the active cell in the DataGrid control. The active cell is the cell that has focus.

back to the top

Description of the Technique

The active cell in the DataGrid control is an instance of the DataGridTextBox class. An instance of the DataGridTextBox class is created and destroyed as focus moves from cell to cell in the DataGrid control.

To access the properties of the active cell, such as the SelectedText property, the SelectionStart property, and the SelectionLength property in this example, you must obtain a reference to the instance of the DataGridTextBox class that is associated with the active cell in the DataGrid control. After you obtain a reference to the active cell, you can access any available property or method.

To define column styles for the columns in the DataGrid control, you must create a DataGridTextBoxColumn object for each column in the DataGrid control. You must then set the MappingName property and the HeaderText property of the object and add the object to the GridColumnStyles collection.

Before you define a column style, you must first define a DataGridTableStyle object to contain the column styles. The example in this article uses the Microsoft SQL Server Authors table.

back to the top

Define a TableStyle object

To define a DataGridTableStyle object that denotes the table style, use the following code.
DataGridTableStyle * ts = new DataGridTableStyle();
ts->MappingName=S"authors";
back to the top

Define a ColumnStyle object

To define a DataGridTextBoxColumn object that denotes the individual column styles, use the following code.
DataGridTextBoxColumn * style1=new DataGridTextBoxColumn();
style1->MappingName=S"au_ID";
style1->HeaderText=S"Author ID";
ts->GridColumnStyles->Add(style1);
Note Although this code illustrates only one definition, you must define a DataGridTextBoxColumn object to hold the column styles for each column in the DataGrid control.

back to the top

Add the TableStyle object to the TableStyles collection of the DataGrid control

To add the DataGridTableStyle object to the TableStyles collection of the DataGrid control, use the following code.
dataGrid1->TableStyles->Add(ts);
The order that you create the DataGridTableStyle object and the DataGridTextBoxColumn object is very important. You must do these tasks in the following order:
  1. Create the DataGridTableStyle object.
  2. Add the DataGridTextBoxColumn objects to the GridColumnStyles collection of the DataGridTableStyle object.
  3. Add the DataGridTableStyle object to the TableStyles collection of the DataGrid control.
back to the top

Obtain a reference to the active cell

To obtain a reference to the active cell, use the following code.
int column = this->dataGrid1->get_CurrentCell().get_ColumnNumber();
DataGridTextBoxColumn * c = __try_cast<DataGridTextBoxColumn *>(this->dataGrid1->TableStyles->get_Item(0)->GridColumnStyles->get_Item(column));
back to the top

Access a property

To access a property, use the following code.
MessageBox::Show(String::Concat(S"Selection Length: \n", c->get_TextBox()->get_SelectionLength().ToString()));
back to the top

Step-by-step example

  1. Create a Microsoft Windows Forms Application (.NET) project by using Microsoft Visual C++ .NET or Microsoft Visual C++ 2005:
    1. Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.
    2. On the File menu, point to New, and then click Project.

      The New Project dialog box appears.
    3. Under Project Types, click Visual C++ Projects.

      Note In Visual Studio 2005, click Visual C++ under Project Types.
    4. Under Templates, click Windows Forms Application (.NET).

      Note In Visual Studio 2005, click Windows Forms Application under Templates.
    5. In the Name box, type DataGridVC, and then click OK.

      By default, the Form1 form is created.
  2. Add a new connection to the Microsoft SQL Server database to your project:
    1. On the View menu, click Server Explorer.
    2. On the Server Explorer tab, right-click Data Connection, and then click Add Connection.

      The Data Link Properties dialog box appears.
    3. In the Data Link Properties dialog box, create a connection to the Pubs sample database.
  3. Expand the node for the connection that you created in step 2, and then expand the Tables node.
  4. Add the authors table from the Tables node to the Form1 form.

    Notice that this step adds the SqlConnection object that is named sqlConnection1 and the SqlDataAdapter object that is named sqlDataAdapter1 to the component tray of the form.
  5. On the Data menu, click Generate Dataset.

    The Generate Dataset dialog box appears.
  6. In the Generate Dataset dialog box, click OK to accept the following default settings:
    • Add a new DataSet object that is named DataSet1.
    • The selected item is authors (SqlDataAdapter1).
    • The Add this Dataset to the Designer check box is selected.
    Notice that the name of the DataSet object is "DataSet1," but the name that is used to identify this DataSet object in the code is "dataSet11." In the component tray, dataSet11 is the name that appears and that identifies this DataSet object.
  7. Add a DataGrid control to the Form1 form.

    By default, the DataGrid control is named DataGrid1.
  8. Right-click the DataGrid1 control, and then click Properties.
  9. In the Properties window of the DataGrid1 control, set the DataSource property to dataSet11, and then set the DataMember property to authors.
  10. Double-click the Form1 form, and then add the following code in the Form1_Load event handler.
    DataGridTableStyle * ts = new DataGridTableStyle();
    ts->MappingName=S"authors";
    			
    DataGridTextBoxColumn * style1=new DataGridTextBoxColumn();
    style1->MappingName=S"au_ID";
    style1->HeaderText=S"Author ID";
    ts->GridColumnStyles->Add(style1);
    
    DataGridTextBoxColumn * style2=new DataGridTextBoxColumn();
    style2->MappingName=S"au_fname";
    style2->HeaderText=S"First Name";
    ts->GridColumnStyles->Add(style2);
    
    DataGridTextBoxColumn * style3=new DataGridTextBoxColumn();
    style3->MappingName=S"au_lname";
    style3->HeaderText=S"Last Name";
    ts->GridColumnStyles->Add(style3);
    				
    dataGrid1->TableStyles->Add(ts);
    		
    sqlDataAdapter1->Fill(dataSet11);
    Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the whole code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
    1. Click Project, and then click <ProjectName> Properties.

      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.
    For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

  11. On the View menu, click Designer to switch to design view.
  12. Right-click the DataGrid1 control, and then click Properties.
  13. In the Properties window, click the Events button (the "lightning bolt" button) to view the events for the control.
  14. Double-click the HelpRequested event.
  15. Add the following code to the dataGrid1_HelpRequested event.
    int column = this->dataGrid1->get_CurrentCell().get_ColumnNumber();
    DataGridTextBoxColumn * c = __try_cast<DataGridTextBoxColumn *>(this->dataGrid1->TableStyles->get_Item(0)->GridColumnStyles->get_Item(column));
    
    switch (column)
    {
    	case 0:    		
    		//Display the SelectionStart property if column is 0.
    		MessageBox::Show(String::Concat(S"Selection Start: \n" , c->get_TextBox()->get_SelectionStart().ToString()));
    	break;
    	case 1:
    		//Display the SelectionLength property if column is 1.
    		MessageBox::Show(String::Concat(S"Selection Length: \n", c->get_TextBox()->get_SelectionLength().ToString()));
    	break;
    	case 2:
    		//Display the SelectedText property if column is 2.
    		MessageBox::Show(String::Concat(S"Selected Text: \n", c->get_TextBox()->get_SelectedText()));
    	break;
    }
    
    hlpevent->Handled  = true;
    Note The HelpRequested event is not an event that you would typically use for this purpose. This example uses the HelpRequested event to avoid developing a contrived situation to demonstrate this code.
  16. Press CTRL+SHIFT+S to save the project.
  17. Press CTRL+SHIFT+B to build the solution.
  18. Press CTRL+F5 to run the project.
  19. Click any cell in the data grid, and then press F1.

    Different properties appear in the message box, depending on the column of the cell that you clicked.
back to the top

REFERENCES

For more information about DataGridTextBoxColumn class members, visit the following Microsoft Developer Network (MSDN) Web site:back to the top

Modification Type:MajorLast Reviewed:1/12/2006
Keywords:kbWindowsForms kbstyle kbForms kbDataBinding kbDatabase kbDataAdapter kbControl kbCollections kbProperties kbHOWTOmaster KB816193 kbAudDeveloper