How change the DataGridView CurrentRow Index ?

Hi,

I just realized that the CurrentRow.Index property is read-only, I need to select the rowindex programatically since the DataGridView is bounded.

That property was really helpfull when it was possible to assign a value, how can I achieve this ?

My problem is that I change the rows order in datatable, I am able to select the new row, but until now, I cannot select the row index, this remains in the original position.

Thanks in advance.

George
[484 byte] By [GeorgeWaters] at [2007-12-28]
# 1

not sure what the issue is it seems to work fine and indexes are right for me.,

Public Class Form2

Private Sub Label1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Label1.Click

'count should be = no of rows - 1

Me.Label1.Text = Me.DataGridView1.CurrentRow.Index

End Sub

Private Sub Label2_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles Label2.Click

'Count should be = no of rows

Me.Label2.Text = Me.DataGridView1.RowCount

End Sub

Private Sub Label3_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles Label3.Click

'Here I forced the dgv to go to the last row.

Me.DataGridView1.CurrentCell = Me.DataGridView1(0, Me.DataGridView1.RowCount - 1)

End Sub

End Class

hrubesh at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2

Hi hrubesh,

Thanks for your post, but using current cell didn't work, I think I have to be clearer, this is what I need:

Let's suppose I have a datagridview with 5 rows and I need to programatically select row no.3, I can do that using:

DataGridView1.Rows(rowIndexFromMouseDown + 1).Selected = True

Where rowIndexFromMouseDown + 1 is the row selected, but even when it is selected, the row index is somewhere else (row 0), if I try to show the info from the current selected row, it will show me the info of row 0, so it doesn't matter if the row 3 is selected because the cursor or index is on row 0.

Until now the only way to do this is by clicking the row.

I need something like CurrentRowIndex used in Net 1.1 (I don't have any idea why they got rid of this function).

Any ideas ?

Thanks in advance.

George.

GeorgeWaters at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3

''' <summary>

'''

''' copy and paste all code,

''' This is the best i could imagine. and it works.

'''

''' </summary>

Public Class Class2

Inherits Form

Private Sub InitializeComponent()

Me.DataGridView1 = New System.Windows.Forms.DataGridView

Me.Button1 = New System.Windows.Forms.Button

Me.Button2 = New System.Windows.Forms.Button

CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()

Me.SuspendLayout()

'

'DataGridView1

'

Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize

Me.DataGridView1.Dock = System.Windows.Forms.DockStyle.Fill

Me.DataGridView1.Location = New System.Drawing.Point(0, 46)

Me.DataGridView1.Name = "DataGridView1"

Me.DataGridView1.Size = New System.Drawing.Size(292, 227)

Me.DataGridView1.TabIndex = 0

'

'Button1

'

Me.Button1.Dock = System.Windows.Forms.DockStyle.Top

Me.Button1.Location = New System.Drawing.Point(0, 0)

Me.Button1.Name = "Button1"

Me.Button1.Size = New System.Drawing.Size(292, 23)

Me.Button1.TabIndex = 1

Me.Button1.Text = "Button1"

Me.Button1.UseVisualStyleBackColor = True

'

'Button2

'

Me.Button2.Dock = System.Windows.Forms.DockStyle.Top

Me.Button2.Location = New System.Drawing.Point(0, 23)

Me.Button2.Name = "Button2"

Me.Button2.Size = New System.Drawing.Size(292, 23)

Me.Button2.TabIndex = 2

Me.Button2.Text = "Button2"

Me.Button2.UseVisualStyleBackColor = True

'

'Class2

'

Me.ClientSize = New System.Drawing.Size(292, 273)

Me.Controls.Add(Me.DataGridView1)

Me.Controls.Add(Me.Button2)

Me.Controls.Add(Me.Button1)

Me.Name = "Class2"

CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()

Me.ResumeLayout(False)

End Sub

Friend WithEvents Button1 As System.Windows.Forms.Button

Friend WithEvents Button2 As System.Windows.Forms.Button

Friend WithEvents DataGridView1 As System.Windows.Forms.DataGridView

Class test

Private b As String

Property a() As String

Get

Return b

End Get

Set(ByVal value As String)

b = value

End Set

End Property

Public Sub New(ByVal r As String)

b = r

End Sub

End Class

Public Sub New()

Me.InitializeComponent()

Dim bs As New BindingSource

Dim v As New List(Of test)

For i As UInt16 = 0 To 10

v.Add(New test(i))

Next

bs.DataSource = v

Me.DataGridView1.DataSource = bs

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Set selected and current - only 1 row is selected at a time.

Dim s As Integer

s = New Random().NextDouble * 10.0

DataGridView1.Rows(s).Selected = True

DataGridView1.CurrentCell = DataGridView1(0, s)

MsgBox(DataGridView1.CurrentCell.RowIndex)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

'Set selected and current - Previously selected rows are kept selected.

Dim s As Integer

s = New Random().NextDouble * 10.0

Dim temp As DataGridViewSelectedRowCollection = Me.DataGridView1.SelectedRows

DataGridView1.CurrentCell = DataGridView1(0, s)

For Each tr As DataGridViewRow In temp

tr.Selected = True

Next

DataGridView1.Rows(s).Selected = True

MsgBox(DataGridView1.CurrentCell.RowIndex)

End Sub

End Class

hrubesh at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
You can use the DataGridView.HitTestInfo to figure out what row the mouse is over.
KenTucker at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5

Hi Ken,

Unfortunately I can't use the mouse, that is I need to move the CurrentRowIndex using buttons, like "up" and "down", if the CurrentRowIndex = 2 and I push the "down" button I need to change the CurrentRowIndex = CurrentRowIndex + 1

Any ideas ?

GeorgeWaters at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6
Use the BindingSource.Position or CurrencyManger.Position to get the current row. Changing the position will move to the next row.
KenTucker at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 7

Hi George,

As i seen you problem. I have also same problem but i solved it. When you click bottom "Down ward" You need to find first current row index then What i do this code is like this

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.AddNewUserDataGridView.CurrentCell = Me.AddNewUserDataGridView.Item(0, Me.AddNewUserDataGridView.CurrentCell.RowIndex + 1)
End Sub

I hope this can help you

saillung at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 8

Per Microsoft: To change the current row, you must set the CurrentCell property to a cell in the desired row.

If your current row index is 0

and you want to change your current index to row 2

dataGridView1.CurrentCell = dataGridView1.Rows[2].Cells[0];

It should work. Good luck

H.Pham at 2007-9-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...