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
topDescription 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 topDefine 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 topDefine 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 topAdd 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:
- Create the DataGridTableStyle object.
- Add the DataGridTextBoxColumn objects to the GridColumnStyles collection of the DataGridTableStyle object.
- Add the DataGridTableStyle object to the TableStyles collection of the DataGrid control.
back to the topObtain 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 topAccess 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 topStep-by-step example
- Create a Microsoft Windows Forms Application
(.NET) project by using Microsoft Visual C++ .NET or Microsoft Visual C++ 2005:
- Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.
- On the File menu, point to
New, and then click Project.
The
New Project dialog box appears. - Under Project Types, click
Visual C++ Projects.
Note In Visual Studio 2005, click Visual C++ under Project Types. - Under Templates, click Windows
Forms Application (.NET).
Note In Visual Studio 2005, click Windows
Forms Application under Templates. - In the Name box, type
DataGridVC, and then click
OK.
By default, the Form1 form is
created.
- Add a new connection to the Microsoft
SQL Server database to your project:
- On the View menu, click Server
Explorer.
- On the Server Explorer tab, right-click Data
Connection, and then click Add Connection.
The Data Link Properties dialog
box appears. - In the Data Link Properties dialog
box, create a connection to the Pubs sample database.
- Expand the node for the connection that you created in step
2, and then expand the Tables node.
- 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. - On the Data menu, click Generate
Dataset.
The Generate Dataset dialog
box appears. - 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. - Add a DataGrid control to the Form1 form.
By default, the DataGrid control is named DataGrid1. - Right-click the DataGrid1 control, and then
click Properties.
- In the Properties window of the DataGrid1 control, set
the DataSource property to dataSet11, and
then set the DataMember property to
authors.
- 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:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> 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: - 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 "lightning bolt" button) to view the events for the control.
- Double-click the HelpRequested
event.
- 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. - Press CTRL+SHIFT+S to save the
project.
- Press CTRL+SHIFT+B to build the
solution.
- Press CTRL+F5 to run the
project.
- 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