Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting from Datagrid. 1

Status
Not open for further replies.

GerardMcL

Technical User
Aug 5, 2004
212
IE
HI,

I want to select a row in a datagrid and transfer that info to text boxes. In VB6 I would have used
DataGrid1_RowColChange()
and then
txtBox1.Text = DataGrid1.Col(1).Text

But these options arent in VB.Net, has anyone any helpful tips or links.

Incidentally anyone knowing of a website that offers a good VB.net tutorial it would be most appreciated, Cheers!
 
Gerard,

DougP sent this to me once when I asked the same question

Private Sub DataGrid1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Click

Dim currRow As DataRow
Dim myColumn As DataColumn
Dim dataGridTable As DataTable
dataGridTable = CType(DataGrid1.DataSource, DataTable)
' Set the current row using the RowNumber property of the CurrentCell.
currRow = dataGridTable.Rows(DataGrid1.CurrentRowIndex)
myColumn = dataGridTable.Columns(0) 'column number 0 is fisrt column
' Get the value of the column 1 in the DataTable.
Label2.Text = currRow(myColumn, DataRowVersion.Current).ToString()
End Sub



Or text1.text= Me.DataGrid1.CurrentCell


Or assign vars

thecell = DataGrid1.CurrentCell
therow = thecell.RowNumber
thecol = thecell.ColumnNumber


Hope this helps

DASHLEY

 
You can use this also:

Dim strName As String
strName = DataGrid1.Item(DataGrid1.CurrentRowIndex, 2)

TextBox1.Text = strName

The "2" is for column #3. Remember it starts with zero so 2 is the 3rd column.

I am new to vb.net also so if this will cause problems please some of you veteran .net programmers let us know.

 
GerardMcL

If you are populating the datagrid with a datatable, you can use a CurrencyManager and a DataRowView:

'Declare these globally to the form with the grid
Dim cm As CurrencyManager
Dim drv As DataRowView

'****************
'populate the datagrid
DataGrid1.DataSource = DataSet1.Tables(0)

'Put this line of code right after you populate the datagrid
cm = DataGrid1.BindingContext(DataSet1.Tables(0))


Then in the grid's MouseUp event:

Private Sub DataGrid1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseUp

drv = CType(cm.Current, DataRowView)

End Sub

You can then reference any of the selected row's fields like so:

drv.Item(0) 'with the field's index number

-or-

drv.Item("FieldName") 'with the actual field name

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
i want to know how to start using Visual Basic.net in designing the website
 
Cheers lad you have been a great help!

But I cant get an answer to this anywhere:

Does VB.Net have a version of the DataReport Designer you got in VB6?

I bought the single VisualBasic.Net package not the entire Visual Studio.Net.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top