Microsoft KB Archive/321293

= How to access properties of the current cell in the datagrid by using Visual C# .NET =

Article ID: 321293

Article Last Modified on 9/15/2005

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition

-



This article was previously published under Q321293





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



SUMMARY
This step-by-step article demonstrates how to obtain a reference to the current cell in the DataGrid control.

Description of the technique
The active cell in the DataGrid is an instance of the DataGridTextBox class and is created and destroyed as focus moves from cell to cell in the DataGrid. To access the properties of the current cell, such as the SelectedText, the SelectionStart, and the SelectionLength properties that are used in this example, you must obtain a reference to the instance of the DataGridTextBox that is associated with the current cell in the DataGrid. After you obtain a reference to the current cell, you can access any available property or method.

To do this, you must create a ColumnStyle for each of the columns in the DataGrid. You must set the ColumnStyle to DataGridTextBoxColumn. To define a ColumnStyle, you must first define a TableStyle to contain the ColumnStyles. The example in this article uses the Microsoft SQL Server Authors table.

Define a TableStyle
To define a TableStyle, use the following code: DataGridTableStyle ts=new DataGridTableStyle; ts.MappingName=&quot;authors&quot;;

Define a ColumnStyle
To define a ColumnStyle, use the following code: DataGridTextBoxColumn style1=new DataGridTextBoxColumn; style1.MappingName=&quot;au_ID&quot;; style1.HeaderText=&quot;Author ID&quot;; ts.GridColumnStyles.Add(style1); Note Although this code only illustrates one definition, you must define a ColumnStyle for each column in the DataGrid.

Add the TableStyle to the TableStyles collection of the DataGrid
To add the TableStyle to the TableStyles collection of the DataGrid, use the following code: dataGrid1.TableStyles.Add(ts); Order is very important. You must create the TableStyle and then add the ColumnStyles to the TableStyle before you add the TableStyle to the TableStyles collection of the DataGrid.

Obtain a reference to the current cell
To obtain a reference to the current cell, use the following code: int column = this.dataGrid1.CurrentCell.ColumnNumber; DataGridTextBoxColumn c = (DataGridTextBoxColumn) this.dataGrid1.TableStyles[0].GridColumnStyles[column];

Access a property
To access a property, use the following code: MessageBox.Show(c.TextBox.SelectionLength.ToString );

Step by step example
 Follow these steps to create a new Windows application in Visual C# .NET:  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. Click Visual C# Projects under Project Types, and then click Windows Application under Templates. By default, Form1 is created.  Follow these steps to add a new connection to the Microsoft SQL Server database:  On the Server Explorer tab, right-click Data Connection, and then click Add Connection.</li> In the Data Link Properties dialog box, create a connection to the Pubs sample database.</li></ol> </li> Expand the node for the connection that you created in step 2, and then expand the Tables node.</li> Drag the authors table from the Tables node to Form1. Notice that this adds the SqlConnection object named sqlConnection1 and the SqlDataAdapter object named sqlDataAdapter1 to the component tray of the form.</li> On the Data menu of the Visual Studio .NET integrated development environment (IDE), click Generate Dataset.</li> In the Generate Dataset dialog box, click OK to accept the following default settings: <ul> Add a new DataSet object that is named DataSet1.</li> The selected item is authors (SqlDataAdapter1).</li> The Add this Dataset to the Designer check box is selected.</li></ul>

Note that the name of the DataSet is DataSet1, but the name that is used to identify this DataSet in code is DataSet11. DataSet11 is the name that appears and that identifies this DataSet in the component tray.</li> Drag the DataGrid control from the toolbox to Form1. By default, the DataGrid is named DataGrid1.</li> In the Property window of DataGrid1, set the DataSource property to DataSet11, and then set the DataMember property to authors.</li> Double-click Form1 to open the code window.</li>  Add the following code in the Form_Load event: DataGridTableStyle ts=new DataGridTableStyle; ts.MappingName=&quot;authors&quot;; DataGridTextBoxColumn style1=new DataGridTextBoxColumn; style1.MappingName=&quot;au_ID&quot;; style1.HeaderText=&quot;Author ID&quot;; ts.GridColumnStyles.Add(style1);

DataGridTextBoxColumn style2=new DataGridTextBoxColumn; style2.MappingName=&quot;au_fname&quot;; style2.HeaderText=&quot;First Name&quot;; ts.GridColumnStyles.Add(style2);

DataGridTextBoxColumn style3=new DataGridTextBoxColumn; style3.MappingName=&quot;au_lname&quot;; style3.HeaderText=&quot;Last Name&quot;; ts.GridColumnStyles.Add(style3); dataGrid1.TableStyles.Add(ts); sqlDataAdapter1.Fill(dataSet11); </li> Open the Properties window for DataGrid1, and then click the lightning button to display the events for the control.</li>  Add the following code to the dataGrid1_HelpRequested event: int column = this.dataGrid1.CurrentCell.ColumnNumber; DataGridTextBoxColumn c = (DataGridTextBoxColumn) this.dataGrid1.TableStyles[0].GridColumnStyles[column];

switch (column) {   case 0: //Display the SelectionStart property if column is 0. MessageBox.Show(&quot;Selection Start: \n&quot; + c.TextBox.SelectionStart.ToString); break; case 1: //Display the SelectionLength property if column is 1. MessageBox.Show(&quot;Selection Text: \n&quot; + c.TextBox.SelectionLength.ToString ); break; case 2: //Display the SelectedText property if column is 2. MessageBox.Show(&quot;Selected Text: \n&quot; + c.TextBox.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 the need for developing a contrived situation to demonstrate this code. </li> <li>Build and run the project.</li> <li>Select text in the data grid, and then press F1. Different properties are displayed in the message box, depending on which column you selected.</li></ol>

Keywords: kbhowtomaster KB321293

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.