I am using Textbox1.Text = DataGridView1.RowCount()
to display(in Textbox1) the total number of items in the datagridview on my vb.net app. Now i want Textbox2 to display the position at the datagridview. How will i do that? Eg I want something like 6 out of 50. So "6" will be displayed at Textbox1. Are you getting me?
Asked
Active
Viewed 2,193 times
0

Henry Gathigira
- 265
- 4
- 11
-
show us what you have tried – user1234433222 May 17 '16 at 20:31
-
Possible duplicate of [Index of Currently Selected Row in DataGridView](http://stackoverflow.com/questions/3578144/index-of-currently-selected-row-in-datagridview) – Nick May 17 '16 at 20:34
-
@ Werdna Textbox2.Text = DataGridView1.CurrentRow() but am getting Error 1 Value of type 'System.Windows.Forms.DataGridViewRow' cannot be converted to 'String'. – Henry Gathigira May 17 '16 at 20:35
-
why not try making an integer and then converting the integer to a string with a veritable – user1234433222 May 17 '16 at 20:39
3 Answers
1
You should handle that in the below event
Private Sub dgv_SelectionChanged(sender As Object, e As EventArgs) Handles dgv.SelectionChanged
If dgv.CurrentRow isnot Nothing
Textbox1.Text = dgv.CurrentRow.Index + 1
End If
End Sub

Ed_
- 973
- 11
- 25
-
this is working but 1. It seems its starting to count from zero and i would like it to start counting at one. 2. I wish this could be shown in a textbox instead of a messagebox. Kindly assist. – Henry Gathigira May 17 '16 at 21:35
-
@HenryGathigira I updated the code; in programming the count usually starts from 0 – Ed_ May 17 '16 at 21:37
-
Awsome. You are the Best. And a genius too. That's what I wanted. – Henry Gathigira May 17 '16 at 21:41
0
For i As Integer = 0 To DataGridView1.RowCount
i = +i
Textbox1.Text = i
Next

Jason Tyler
- 1,331
- 13
- 26

Claudio
- 1
-1
Use DataGridView.CurrentRow.Index
property.

dotNET
- 33,414
- 24
- 162
- 251
-
Object reference not set to an instance of an object. That's the error am getting. – Henry Gathigira May 17 '16 at 20:42
-
1Use `If` to check if `CurrentRow` is `null`. Come on. That is pretty basic. – dotNET May 17 '16 at 20:44
-
Yeah I know its basic but i am also so new in vb. Did somebody get my question? If code 1 is working i know code 2 can work too. Somebody just assist. I am pretty tired. In other words i want the code behind binding navigator "position" textbox. – Henry Gathigira May 17 '16 at 20:50