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="authors";
Define a ColumnStyle
To define a
ColumnStyle, use the following code:
DataGridTextBoxColumn style1=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.
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.
- 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.
- 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:
DataGridTableStyle ts=new DataGridTableStyle();
ts.MappingName="authors";
DataGridTextBoxColumn style1=new DataGridTextBoxColumn();
style1.MappingName="au_ID";
style1.HeaderText="Author ID";
ts.GridColumnStyles.Add(style1);
DataGridTextBoxColumn style2=new DataGridTextBoxColumn();
style2.MappingName="au_fname";
style2.HeaderText="First Name";
ts.GridColumnStyles.Add(style2);
DataGridTextBoxColumn style3=new DataGridTextBoxColumn();
style3.MappingName="au_lname";
style3.HeaderText="Last Name";
ts.GridColumnStyles.Add(style3);
dataGrid1.TableStyles.Add(ts);
sqlDataAdapter1.Fill(dataSet11);
- Open the Properties window for DataGrid1, and then click the lightning button to display the events for the control.
- 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("Selection Start: \n" + c.TextBox.SelectionStart.ToString());
break;
case 1:
//Display the SelectionLength property if column is 1.
MessageBox.Show("Selection Text: \n" + c.TextBox.SelectionLength.ToString() );
break;
case 2:
//Display the SelectedText property if column is 2.
MessageBox.Show("Selected Text: \n" + 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. - 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.