ARTICLE

Moving focus from one row to other in datagrid in VB.NET

Posted by Satyapriya Nayak Articles | Windows Forms VB.NET September 01, 2011
In this article we will know how to move focus from one row to another row in a datagrid.
Reader Level:

In this article we will know how to move focus from one row to another row in a datagrid. Here we add one datagrid and five buttons controls respectively to the form. First in the Form1_Load event data will be loaded into the datagrid and with the help of four buttons we will navigate the focus from one row to another.

Imports System.Data

Imports System.Data.OleDb

Public Class Form1

    Dim ConnectionString As String = System.Configuration.ConfigurationSettings.AppSettings("dsn")

    Dim con As OleDbConnection = New OleDbConnection(ConnectionString)

    Dim com As OleDbCommand

    Dim oledbda As OleDbDataAdapter

    Dim ds As DataSet

    Dim dt As DataTable

    Dim str As String

   

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Try

            con.Open()

            str = "select * from student"

            com = New OleDbCommand(str, con)

            oledbda = New OleDbDataAdapter(com)

            ds = New DataSet

            oledbda.Fill(ds, "student")

            con.Close()

            dt = ds.Tables("student")

            DataGrid1.ReadOnly = True

            DataGrid1.DataSource = ds

            DataGrid1.DataMember = "student"

            btnenadisi(True)

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

    End Sub

    Private Sub btnfirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfirst.Click

        DataGrid1.UnSelect(DataGrid1.CurrentRowIndex)

        DataGrid1.CurrentRowIndex = 0

        DataGrid1.Select(DataGrid1.CurrentRowIndex)

        btnenadisi(True)

    End Sub

    Private Sub btnprev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprev.Click

        DataGrid1.UnSelect(DataGrid1.CurrentRowIndex)

        DataGrid1.CurrentRowIndex -= 1

        DataGrid1.Select(DataGrid1.CurrentRowIndex)

        btnenadisi(True)

    End Sub

    Private Sub btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnext.Click

        DataGrid1.UnSelect(DataGrid1.CurrentRowIndex)

        DataGrid1.CurrentRowIndex += 1

        DataGrid1.Select(DataGrid1.CurrentRowIndex)

        btnenadisi(True)

    End Sub

    Private Sub btnlast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlast.Click

        DataGrid1.UnSelect(DataGrid1.CurrentRowIndex)

        DataGrid1.CurrentRowIndex = dt.Rows.Count - 1

        DataGrid1.Select(DataGrid1.CurrentRowIndex)

        btnenadisi(True)

    End Sub

    Private Sub btnenadisi(ByVal x As Boolean)

        btnfirst.Enabled = x

        btnprev.Enabled = x

        btnnext.Enabled = x

        btnlast.Enabled = x

        btnclose.Enabled = x

    End Sub

    Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click

        Me.Close()

    End Sub

End Class

Output

satyaimage.gif

Thanks for reading

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor