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
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 =
TrueWhere 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.
'''
<summary>'''
''' copy and paste all code,
''' This is the best i could imagine. and it works.
'''
'''
</summary>Public
Class Class2Inherits FormPrivate Sub InitializeComponent()Me.DataGridView1 = New System.Windows.Forms.DataGridViewMe.Button1 = New System.Windows.Forms.ButtonMe.Button2 = New System.Windows.Forms.ButtonCType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()Me.SuspendLayout()''DataGridView1'Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSizeMe.DataGridView1.Dock = System.Windows.Forms.DockStyle.FillMe.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.TopMe.Button1.Location = New System.Drawing.Point(0, 0)Me.Button1.Name = "Button1"Me.Button1.Size = New System.Drawing.Size(292, 23)Me.Button1.TabIndex = 1Me.Button1.Text = "Button1"Me.Button1.UseVisualStyleBackColor = True''Button2'Me.Button2.Dock = System.Windows.Forms.DockStyle.TopMe.Button2.Location = New System.Drawing.Point(0, 23)Me.Button2.Name = "Button2"Me.Button2.Size = New System.Drawing.Size(292, 23)Me.Button2.TabIndex = 2Me.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 SubFriend WithEvents Button1 As System.Windows.Forms.ButtonFriend WithEvents Button2 As System.Windows.Forms.ButtonFriend WithEvents DataGridView1 As System.Windows.Forms.DataGridViewClass testPrivate b As StringProperty a() As StringGetReturn bEnd GetSet(ByVal value As String)b = value
End SetEnd PropertyPublic Sub New(ByVal r As String)b = r
End SubEnd ClassPublic Sub New()Me.InitializeComponent()Dim bs As New BindingSourceDim v As New List(Of test)For i As UInt16 = 0 To 10v.Add(
New test(i))Nextbs.DataSource = v
Me.DataGridView1.DataSource = bsEnd SubPrivate 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 Integers =
New Random().NextDouble * 10.0DataGridView1.Rows(s).Selected =
TrueDataGridView1.CurrentCell = DataGridView1(0, s)
MsgBox(DataGridView1.CurrentCell.RowIndex)
End SubPrivate 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 Integers =
New Random().NextDouble * 10.0Dim temp As DataGridViewSelectedRowCollection = Me.DataGridView1.SelectedRowsDataGridView1.CurrentCell = DataGridView1(0, s)
For Each tr As DataGridViewRow In temptr.Selected =
TrueNextDataGridView1.Rows(s).Selected =
TrueMsgBox(DataGridView1.CurrentCell.RowIndex)
End SubEnd
ClassHi 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 ?
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
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