Microsoft Knowledge Base Email Alertz

(816193) - 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...

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 816193 - Last Review: May 12, 2007 - Revision: 2.2

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

On This Page

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.

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.

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";

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.

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.

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));

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()));

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 (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.

REFERENCES

For more information about DataGridTextBoxColumn class members, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridtextboxcolumn_members(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridtextboxcolumn_members(vs.71).aspx)

APPLIES TO
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft .NET Framework 1.1
Keywords: 
kbwindowsforms kbstyle kbforms kbdatabinding kbdatabase kbdataadapter kbcontrol kbcollections kbproperties kbhowtomaster KB816193
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate