Results 1 to 2 of 2

Thread: VB.Net Set focus to a Grid View Cell programmatically

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    8

    Question VB.Net Set focus to a Grid View Cell programmatically

    Hello guys

    I am trying to pass values from a 2nd form to main form, and to certain extends I have managed the stuffs. However, Once the values are passed to the datagridview of the main form, I want to move the focus to a certain column and that is not happening.
    Please refer the image for the approach

    Name:  vb1.jpg
Views: 3571
Size:  14.4 KB

    and the code behind with form2.vb as below

    Code:
    Public Class Form2
        Private f1 As Form1
        Public Sub New(ByRef f As Form)
            InitializeComponent()
            Me.f1 = f
        End Sub
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim dt As DataTable = New DataTable
            dt.Columns.Add("itemid")
            dt.Columns.Add("itemname")
    
            dt.Rows.Add(1000, "isceon 22/50")
            dt.Rows.Add(1001, "Freon 22/50")
            dt.Rows.Add(1002, "Mafatlal 22/50")
            dt.Rows.Add(1003, "Access Value 1/4")
            dt.Rows.Add(1004, "Compressor 5hp")
    
            DataGridView1.DataSource = dt
    
    
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'If (f1.DataGridView1.CurrentRow.IsNewRow) Then
            '    Dim cell As DataGridViewCell = f1.DataGridView1.CurrentRow.Cells("itemid")
            '    DataGridView1.CurrentCell = cell
            '    DataGridView1.BeginEdit(True)
            'End If
    
            f1.DataGridView1.CurrentRow.Cells("itemid").Value = Me.DataGridView1.CurrentRow.Cells("itemid").Value
            f1.DataGridView1.CurrentRow.Cells("itemname").Value = Me.DataGridView1.CurrentRow.Cells("itemname").Value
        End Sub
    End Class
    I know I cannot set the focus to the desired cell because the control is not initialized. Please explain me how to!


    Please help!


    regards,

  2. #2

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    8

    Re: VB.Net Set focus to a Grid View Cell programmatically

    Okay guys, I got it done (don't know whether it is the right approach yet, however it does support my requirements)

    Code bit as below

    Code:
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            If (f1.DataGridView1.CurrentRow.IsNewRow) Then
                f1.DataGridView1.CurrentRow.Cells("itemid").Value = Me.DataGridView1.CurrentRow.Cells("itemid").Value
                Dim cell As DataGridViewCell = f1.DataGridView1.CurrentRow.Cells("itemname")
                f1.DataGridView1.CurrentCell = cell
                f1.DataGridView1.BeginEdit(True)
                f1.DataGridView1.CurrentCell.Selected = True
            End If
    
    
        End Sub
    
    This way, the button click copies the current form values to main form and sends the focus to the said column.
    
    regards,

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width