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 = "authors"
back to the top
Define a ColumnStyle
To define a
ColumnStyle, use the following code:
Dim style1 As DataGridTextBoxColumn = New DataGridTextBoxColumn()
style1.MappingName = "au_ID"
style1.HeaderText = "Author ID"
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.
- 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.
- Expand the node for the connection that you created in step 2, and then expand the Tables node.
- 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.
- On the Data menu of the Visual Studio .NET integrated development environment (IDE), click Generate Dataset.
- 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.
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. - Drag the DataGrid control from the toolbox to Form1. By default, the DataGrid is named DataGrid1.
- In the Property window of DataGrid1, set the DataSource property to DataSet11, and then set the DataMember property to authors.
- Double-click Form1 to open the code window.
- Add the following code in the Form_Load event:
Dim ts As DataGridTableStyle = New DataGridTableStyle()
ts.MappingName = "authors"
Dim style1 As DataGridTextBoxColumn = New DataGridTextBoxColumn()
style1.MappingName = "au_ID"
style1.HeaderText = "Author ID"
ts.GridColumnStyles.Add(style1)
Dim style2 As DataGridTextBoxColumn = New DataGridTextBoxColumn()
style2.MappingName = "au_fname"
style2.HeaderText = "First Name"
ts.GridColumnStyles.Add(style2)
Dim style3 As DataGridTextBoxColumn = New DataGridTextBoxColumn()
style3.MappingName = "au_lname"
style3.HeaderText = "Last Name"
ts.GridColumnStyles.Add(style3)
dataGrid1.TableStyles.Add(ts)
sqlDataAdapter1.Fill(DataSet11)
-
In the drop-down list at the top left of the code window, select Datagrid1.
-
In the drop-down list at the top right of the code window, select the HelpRequested event.
-
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("Selection Start:" & vbCrLf & _
c.TextBox.SelectionStart.ToString())
Case 1
'Show the SelectionLength property if column is 1.
MessageBox.Show("Selection Text:" & vbCrLf & _
c.TextBox.SelectionLength.ToString())
Case 2
'Show SelectedText property if column is 2.
MessageBox.Show("Selected Text:" & 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.
- Build and run the project.
- Select text in the data grid, and then press F1. Different properties are displayed in the message box, depending on which column you selected.
back to the top