How do you refresh a datagrid without it loosing focus?

emaduddeen

Well-known member
Joined
May 5, 2010
Messages
171
Location
Lowell, MA & Occasionally Indonesia
Programming Experience
Beginner
Greeting Everyone.

I have a few questions.

First one is: How do you refresh a datagrid without it loosing focus?

Currently this is the code I use to get data into the datagrid:

VB.NET:
    Private Sub InitializeTheDataGrid()
        ' Declare strings to hold SQL Statements.
        '----------------------------------------
        strSqlStatement = _
            "SELECT attendance.AttendanceID, " & _
                   "class.ClassID, " & _
                   "class.ClassName, " & _
                   "Student.StudentId, " & _
                   "LTrim(Student.FirstName) & ' ' & LTrim(Student.LastName) AS [Student Name], " & _
                   "Int((Date()-dob)/365.25) AS StudentAge, " & _
                   "Parent.HomePhone, " & _
                   "Parent.Email, " & _
                   "attendance.DateOfClass, " & _
                   "attendance.Absent, " & _
                   "class.GradeID " & _
              "FROM (Class INNER JOIN " & _
              "(Attendance INNER JOIN Student ON Attendance.StudentID = Student.StudentId) " & _
              "ON Class.ClassId = Attendance.ClassId) INNER JOIN Parent ON Student.ParentId = Parent.ParentId"

        Try
            ' Create the connection object to use an SQL query and open it.
            '--------------------------------------------------------------
            objConnection = New OleDbConnection(My.Settings.ISGL_Database)
            objConnection.Open()

            ' Create a DataTable object to hold data from the SQL query.
            '-----------------------------------------------------------
            objDataTable = New DataTable()

            ' Create a DataAdapter object for the DataTable.
            '-----------------------------------------------
            objDataAdapter = New OleDbDataAdapter(strSqlStatement, objConnection)

            ' Load the DataAdapter with the data from the DataTable SQL query.
            '-----------------------------------------------------------------
            objDataAdapter.Fill(objDataTable)

            ' Load the data into the grid.
            '-----------------------------
            LightGridAttendance.DataSource = objDataTable

        Finally
            ' Close the connection if it's currently open.
            '---------------------------------------------
            If objConnection IsNot Nothing Then
                objConnection.Close()
            End If
        End Try
    End Sub

Once the data is loaded in the grid the application will delete, insert or update the database.

At that point I issue a call to the subprocedure with InitializeTheDataGrid.

The line of code that makes the datagrid to loose focus is LightGridAttendance.DataSource = objDataTable

Is there any way to stop it from loosing focus?

The other question is that since I'm rather new to VB 2008 I'm using the "New" keyword in many places such as objDataAdapter = New OleDbDataAdapter(strSqlStatement, objConnection)

Will the application eventually use up some kind of limit by repeated use of the "New" keyword?

I also use LightGridAttendance.Focus() to get focus on the datagrid but I think that statement does not work with datagrids. Nothing happens.

Is there a way to refresh the datagrid data without using LightGridAttendance.DataSource = objDataTable ?

All help will be greatly appreciated.

Truly,
Emad
 
The grid doesn't lose focus, which is why calling its Focus method doesn't do anything. What happens is that all the existing rows are removed, so the previous current cell and current row are no longer selected. When you add new rows, they won't be automaticlly selected because they aren't the same rows. You need to remember what data is selected before you refresh and then reselect it manually yourself afterwards.
 
Greetings jmcilhinney,

Thanks for the reply.

Could you provide some sample coding to show me how to reselect the data. I can store the primary key and use that to find it if you can show me how to apply it in re-selecting the data. The primary key is AttendanceID.

Truly,
Emad
 
Bind your data to the grid using a BindingSource. You can call its Find method to get the index of a row that satisfies a specific condition. You can then assign that index to the Position property of the BindingSource.
 
Back
Top