How To Set Focus on a Column in a DataGrid Control (293348)



The information in this article applies to:

  • Microsoft Visual Basic Professional Edition for Windows 6.0

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

  1. Create a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. On the Project menu, click References, and then click Microsoft ActiveX Data Objects 2.x Library to add a project reference.
  3. 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.
  4. Add the following code to the Form_Load event:
    Me.DataGrid1.Col = 1
    					

Set Up the ActiveX Data Objects Data Control

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

Set Up the DataGrid Control

  1. Click the DataGrid, and then set the DataSource property to adodc1.
  2. Right-click Retrieve Fields.
  3. Click Yes to replace existing grid layout with field definitions.
  4. Resize the grid to show all of the fields.
  5. Right-click the grid, and then click Properties.
  6. 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.
  7. On the Keyboard tab, in the TabAction drop-down combo box, click 2-drgGridNavigation, and then click OK to save the changes.
  8. Add the following code to the DataGrid1_RowColChange event:
       Me.Adodc1.Recordset.MoveNext
       Me.DataGrid1.Col = 1 
    					
  9. Run the application, and press the TAB key to move through the au_lname column.

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

128205 How To Set Buffering Mode in Visual FoxPro


Modification Type:MinorLast Reviewed:7/13/2004
Keywords:kbCtrl kbhowto KB293348