0

My datagridview[Resident's Information DatagridView] has a weird behavior. When I try to run my application using Debug in Visual Studio It takes so much time before Datagridview loads fully but when I try to run the program through the external .exe under the Debug folder the datagridview loads fast and no delay. I tried cleaning the project or the solution but to no avail. I also tried building it again.

for reference or much detail see video below:

running the application through visual studio https://drive.google.com/file/d/1LbM--rigXy-m17e7v1DAdmxGnMQ7gE4g/view?usp=share_link

running the application through the .exe file in the Debug folder https://drive.google.com/file/d/1b19jGooDXMjTkaz5aGMYnimyRlikCxhL/view?usp=share_link

Here's how I populate my DataGridView

mySQLCommand.CommandText = "Select * From residents" & (IIf(txtSearchResident.Text.Trim = "" Or txtSearchResident.Text = "Type in your search", " ", " WHERE first_name LIKE @resident_name OR middle_name LIKE @resident_name OR last_name LIKE @resident_name")) & " order by first_name asc limit 30 OFFSET " & (((CInt(Me.txtPageNoResident.Text)) - 1) * 30)
                mySQLCommand.Parameters.AddWithValue("@resident_name", "%" & txtSearchResident.Text & "%")
                mySQLReader = mySQLCommand.ExecuteReader

While mySQLReader.Read
                        Dim middle, ext As String
                        If mySQLReader!middle_name <> DBNull.Value Then
                            middle = mySQLReader!middle_name + " "
                        Else
                            middle = ""
                        End If

                        If mySQLReader!ext_name <> DBNull.Value Then
                            ext = mySQLReader!ext_name
                        Else
                            ext = ""
                        End If

                        datagrid.Rows.Add(New String() {(mySQLReader!first_name + " " + middle + mySQLReader!last_name + " " + ext), mySQLReader!sex, mySQLReader!is_voter, mySQLReader!contact_no})

                    End While

Language used: Visual Basic .Net Visual Studio version: 2022 Database: MySQL using Xampp

  • Perhaps you could explain what you're actually doing to populate the grid. Have you actually profiled the code to determine exactly where the time is being taken? Please spend some time in the Help Center to learn how to write a proper question. – jmcilhinney Dec 20 '22 at 08:15
  • Don't add the rows one by one. Bind your data. One option is to create a `DataTable` and pass the data reader to its `Load` method, then bind that to the grid, preferably via a `BindingSource`. Another option is to assign the data reader directly to the `DataSource` property of a `BindingSource`, then bind that to the grid. – jmcilhinney Dec 20 '22 at 09:51
  • We haven't used `IIf` since 2008. use the `If` operator. – jmcilhinney Dec 20 '22 at 09:53
  • @jmcilhinney I don't think the problem's is in the way I populate my DatagridView, as I also used that way in my household Datagrid but the datagridview loads with no delay. It is seen in the video – Jayvee Jimenez Dec 20 '22 at 10:59
  • As a general rule, running via the debugger will be slower than running the compiled exe. VS does a whole heap of work to enable debugging. That said, the sql command you're running looks to be pretty darn slow to start off with. Like operators can be slow and you're running multiple. Would also say, don't combo command params and string concatenations in a single command where you can avoid it, params preferred. And look at using Parameters.Add(....) over AddWithValue(). Can get unintended type casts – Hursey Dec 20 '22 at 20:00
  • @Hursey, Thank you sir! Will definitely look into that. – Jayvee Jimenez Dec 21 '22 at 04:54

0 Answers0