Microsoft KB Archive/293348

= How To Set Focus on a Column in a DataGrid Control =

Article ID: 293348

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Professional Edition

-



This article was previously published under Q293348



SUMMARY
There are many times when you may want to set focus on a column in a Microsoft DataGrid Control 6.0. For example, if you have a read-only field, you may not want the TAB key to stop on the read-only fields in the DataGrid. This article describes how to set focus in a Microsoft DataGrid Control 6.0.



MORE INFORMATION
The following steps demonstrate how to control the focus in a Microsoft DataGrid Control 6.0.

Set Up the Project
 Create a new Standard EXE project in Visual Basic. Form1 is created by default. On the Project menu, click References, and then click Microsoft ActiveX Data Objects 2.x Library to add a project reference. On the Project menu, click Components, and then click Microsoft ADO Data Control 6.0 and Microsoft DataGrid Control 6.0 to add these items to the toolbox.  Add the following code to the Form_Load event: Me.DataGrid1.Col = 1 

Set Up the ActiveX Data Objects Data Control
 Drag an ActiveX Data Objects (ADO) control and a DataGrid control, and drop these controls onto Form1. Right-click the ADO DataControl, and then click ADODC Properties.</li> Click Use Connection String, and then click Build.</li> Click Microsoft OLEDB Provider for SQL Server, and then click Next.</li> On the Connection tab, type the connection information for your server, choosing the pubs database.</li> Click Test Connection to verify that the connection is good, and then click OK to close the Properties window.</li> On the RecordSource tab, in the Command Type box, type 1-adCmdText to change the command type.</li>  In the Command Text box, type the following code: select au_id, au_lname from authors </li> Click Apply, and then click OK to close the Property Pages dialog box.</li></ol>

Set Up the DataGrid Control
 Click the DataGrid, and then set the DataSource property to adodc1.</li> Right-click Retrieve Fields.</li> Click Yes to replace existing grid layout with field definitions.</li> Resize the grid to show all of the fields.</li> Right-click the grid, and then click Properties.</li> On the Layout tab, click column 0(au_id) from the drop-down combo box. Select the Locked check box so that the first column of the grid is read-only.</li> On the Keyboard tab, in the TabAction drop-down combo box, click 2-drgGridNavigation, and then click OK to save the changes.</li> <li> Add the following code to the DataGrid1_RowColChange event: Me.Adodc1.Recordset.MoveNext Me.DataGrid1.Col = 1 </li> <li>Run the application, and press the TAB key to move through the au_lname column.</li></ol>

<div class="references_section">