Microsoft KB Archive/816193

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

Article ID: 816193

Article Last Modified on 5/12/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * 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
 * Description of the technique
 * Define a TableStyle object
 * Define a ColumnStyle object
 * Add the TableStyle object to the TableStyles collection of the DataGrid control
 * Obtain a reference to the active cell
 * Access a property
 * Step-by-step example
 * REFERENCES



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&quot;authors&quot;; 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&quot;au_ID&quot;; style1->HeaderText=S&quot;Author ID&quot;; 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 Properties'''.

Note  is a placeholder for the name of the project. Expand Configuration Properties, and then click General. 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

 On the View menu, click Designer to switch to design view. Right-click the DataGrid1 control, and then click Properties. In the Properties window, click the Events button (the &quot;lightning bolt&quot; button) to view the events for the control.</li> Double-click the HelpRequested event.</li>  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&quot;Selection Start: \n&quot;, c->get_TextBox->get_SelectionStart.ToString)); break; case 1: //Display the SelectionLength property if column is 1. MessageBox::Show(String::Concat(S&quot;Selection Length: \n&quot;, c->get_TextBox->get_SelectionLength.ToString)); break; case 2: //Display the SelectedText property if column is 2. MessageBox::Show(String::Concat(S&quot;Selected Text: \n&quot;, 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. </li> Press CTRL+SHIFT+S to save the project.</li> Press CTRL+SHIFT+B to build the solution.</li> Press CTRL+F5 to run the project.</li> 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.</li></ol>

back to the top

<div class="references_section">