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!

Need help with vb.net (Newbie)

Status
Not open for further replies.

RocAFella2007

Technical User
Mar 21, 2007
45
GB
Hi,

Was hoping someone could help me out here please, I have a datagrid and a few textboxes. Whenever a user selects a row from the datagrid I want that data to be shown in the textboxes, everything is working fine but which properties should I use for the datagrid? I have tried 'click' but this only works when the user clicks on the row with the mouse (so if the user uses the up and down arrows on the keyboard it doesn't work.)

Which property should I use? If this is not possible then would I have to disable the keys? Im using vb 2003.

Thanks in advance
 
Use the keypress event. Then in the event you can check the key like:

Code:
If e.KeyChar = ChrW(Keys.Down) Then

-I hate Microsoft!
-Forever and always forward.
 
THanks for your swift reply, however I have tried that but it doesn't seem to work. So far I have the following coding:

If DataGrid1.CurrentRowIndex > -1 Then
Me.ID = CType(DataGrid1.Item( _
DataGrid1().CurrentRowIndex, 0), Int32)
Else
MessageBox.Show("Please Select A Row.", _
"No Row Selected", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End If

Currently Ive set that to use with the click event, however I can still use the keys and the information in the text boxes doesn't change.

regards.
 
I don't really do DataGrids any more and I don't have anything I can quickly setup to test right now, but try this for just keypress then:

Code:
if e.keychar = chrw(keys.down) then
   Dim NewIndex as integer
   cIndex = DataGridView1.SelectedRows.Item(0).Index + 1
   if cIndex < DataGridView1.Rows.Count then
      DataGridView1.Rows.Item(cIndex).Selected = True
   End If
You will want to play around with that a bit as there could be problems. Are DataGridView1 and DataGridView1.SelectedRows actually a Zero based arrays? Microsoft is really inconsistent with that. Also selected rows can have multiple rows so that just says move down 1 from the first selected row. This will do nothing if the row selected is the last row so you have to decide how you want to deal with that.

-I hate Microsoft!
-Forever and always forward.
 
oops
this:
Code:
Dim NewIndex as integer

should be this:
Code:
Dim cIndex as integer

And it is missing the final End If.


-I hate Microsoft!
-Forever and always forward.
 
Code:
	Private Sub DataGridView1_RowStateChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowStateChangedEventArgs) Handles DataGridView1.RowStateChanged

		MessageBox.Show(DataGridView1.CurrentRow.ToString)
		MessageBox.Show(e.Row.ToString)

	End Sub

Either CurrentRow, or e.Row should give you the correct row you will have to check to see which.

NB, this is using 2008. There will almost certainly be a similar event in 2003.


Hope this helps.



[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top