Microsoft KB Archive/321296

= How to access properties of the current cell in the Datagrid by using Visual Basic .NET or Visual Basic 2005 =

Article ID: 321296

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q321296



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

IN THIS TASK
SUMMARY
 * Description of the Technique
 * Define a TableStyle
 * Define a ColumnStyle
 * Add the TableStyle to the TableStyles Collection of the DataGrid
 * Obtain a Reference to the Current Cell
 * Access a Property
 * Step-by-Step Example



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

back to the top

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.

back to the top

Define a TableStyle
To define a TableStyle, use the following code: Dim ts As DataGridTableStyle = New DataGridTableStyle ts.MappingName = &quot;authors&quot; back to the top

Define a ColumnStyle
To define a ColumnStyle, use the following code: Dim style1 As DataGridTextBoxColumn = 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.

back to the top

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.

back to the top

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; Dim column As Integer = Me.dataGrid1.CurrentCell.ColumnNumber Dim c As DataGridTextBoxColumn = _ CType(Me.dataGrid1.TableStyles(0).GridColumnStyles(column), _         DataGridTextBoxColumn) back to the top

Access a Property
To access a property, use the following code: MessageBox.Show(c.TextBox.SelectionStart.ToString) back to the top

Step-by-Step Example
 Follow these steps to create a new Windows application in Visual Basic .NET or in Visual Basic 2005:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Click Visual Basic Projects under Project Types, and then click Windows Application under Templates. By default, Form1 is created.

Note In Visual Studio 2005, click Visual Basic under Project Types.  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.

Note In Visual Studio 2005, create a connection in the Choose Data Source dialog box.</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: Dim ts As DataGridTableStyle = New DataGridTableStyle ts.MappingName = &quot;authors&quot;

Dim style1 As DataGridTextBoxColumn = New DataGridTextBoxColumn style1.MappingName = &quot;au_ID&quot; style1.HeaderText = &quot;Author ID&quot; ts.GridColumnStyles.Add(style1)

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

Dim style3 As DataGridTextBoxColumn = 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> In the drop-down list at the top left of the code window, select Datagrid1.</li> In the drop-down list at the top right of the code window, select the HelpRequested event.</li>  Add the following code to the dataGrid1_HelpRequested event: Dim column As Integer = Me.dataGrid1.CurrentCell.ColumnNumber Dim c As DataGridTextBoxColumn = _ CType(Me.dataGrid1.TableStyles(0).GridColumnStyles(column), _       DataGridTextBoxColumn)

Select Case (column) Case 0 'Show the SelectionStart property if column is 0. MessageBox.Show(&quot;Selection Start:&quot; & vbCrLf & _            c.TextBox.SelectionStart.ToString)

Case 1 'Show the SelectionLength property if column is 1. MessageBox.Show(&quot;Selection Text:&quot; & vbCrLf & _             c.TextBox.SelectionLength.ToString)

Case 2 'Show SelectedText property if column is 2. MessageBox.Show(&quot;Selected Text:&quot; & vbCrLf & _           c.TextBox.SelectedText.ToString)

End Select

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>

back to the top

Keywords: kbvs2005swept kbvs2005applies kbhowtomaster KB321296

-

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

© Microsoft Corporation. All rights reserved.