Home > Software engineering >  [vb SQL] to select a row to the datagrid, make it the first cell content is displayed in the text in
[vb SQL] to select a row to the datagrid, make it the first cell content is displayed in the text in

Time:10-01


Such as this situation, I will need to select the first line, text1. Text=1001
I have to write code:
Option Explicit


Private Sub Command1_Click ()
Dim cn As New ADODB. Connection
Dim rs_findsp As New ADODB. You
Cn. CursorLocation=adUseClient
Cn. The ConnectionString="driver={SQL Server}; Server=(local); Uid=; The PWD=; The database=market "
Cn. Open
Rs_findsp. Open (" select goods number, name of commodity, unit price, product type, manufacturer, inventory from commodities table where type='" & amp; Combo1. Text & amp; "'"), cn, adOpenDynamic adLockOptimistic
The Set DataGrid1. The DataSource=rs_findsp
DataGrid1. Refresh
DataGrid1. AllowAddNew=False
DataGrid1. AllowDelete=False
DataGrid1. AllowUpdate=False
The Set DataGrid1. The DataSource=rs_findsp
End Sub

Private Sub Form_Load ()

Combo1. AddItem "food"
Combo1. AddItem "drink"
Combo1. AddItem "commodity"
Combo1. AddItem "dairy products"
Combo1. AddItem "cigarettes"

End Sub

Is really can't write, after all, a beginner, consult! Thank you very much!

CodePudding user response:

Give you a general method:
 Sub Test () 
Dim iRow As Long, iCol As Long
IRow=2
ICol=1
With DataGrid1
Text1. Text=. The Columns (iCol). CellValue (. GetBookmark (iRow))
End With
End Sub


This method, as long as the specified category is from 0 () can be read into the corresponding value

CodePudding user response:

Is because the DataGrid and binding record set, and select the DataGrid record set corresponding record when each column is selected, so you need to reference records in the corresponding field,

CodePudding user response:

Try to switch to mshflexgrid control:
The set mshflexgrid1. The datasource=recordset

Private sub mshflexgrid1_click ()
Textbox1. Text=mshflexgrid1. Textmatrix (mshflexgrid1 row, 0)
End sub
  • Related