Click here to Skip to main content
15,887,917 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
i have data grid and my database is in ms access i tried to load data in vb.net data grid view but it takes long time to load data. what to do ? i have AllowUesertoAddrow property of datagridview false. and adding rows in code.

What I have tried:

Dim sql = ("SELECT * FROM TotalInput")
        maxrow = get_maxrow(sql)

        Dim i As Integer
        i = dt.Rows.Count - 1
        DgTotalInput.Rows.Add(1)
        For i = 1 To i - 1
            DgTotalInput.Rows.Add(1)
        Next
        For i = 0 To dt.Rows.Count - 1
            With dt.Rows(i)
                DgTotalInput.Rows(i).Cells(0).Value = .Item("ID")
                DgTotalInput.Rows(i).Cells(2).Value = .Item("TInputDate")
                DgTotalInput.Rows(i).Cells(3).Value = .Item("TICredited")
                DgTotalInput.Rows(i).Cells(4).Value = .Item("TIAmount")
                DgTotalInput.Rows(i).Cells(5).Value = .Item("TIMode")
                DgTotalInput.Rows(i).Cells(6).Value = .Item("TIDate")
                DgTotalInput.Rows(i).Cells(7).Value = .Item("TIDebited")
                DgTotalInput.Rows(i).Cells(8).Value = .Item("TInputMode")
                DgTotalInput.Rows(i).Cells(9).Value = .Item("TITotal")
                DgTotalInput.Rows(i).Cells(10).Value = .Item("TIRemark")
            End With
        Next i
Posted
Updated 11-Aug-20 19:52pm

You don't need to load the grid with data at runtime row-by-row.

You can get the data from database, fill it in adapter and then assign the datasource of grid. Believe that should be faster for you.
Example:
VB
Private Sub GetData(ByVal selectCommand As String)
    Try
        ' Specify a connection string.  
        ' Replace <SQL Server> with the SQL Server for your Northwind sample database.
        ' Replace "Integrated Security=True" with user login information if necessary.
        Dim connectionString As String =
            "Data Source=<SQL Server>;Initial Catalog=Northwind;" +
            "Integrated Security=True;"

        ' Create a new data adapter based on the specified query.
        dataAdapter = New SqlDataAdapter(selectCommand, connectionString)

        ' Create a command builder to generate SQL update, insert, and
        ' delete commands based on selectCommand. 
        Dim commandBuilder As New SqlCommandBuilder(dataAdapter)

        ' Populate a new data table and bind it to the BindingSource.
        Dim table As New DataTable With {
            .Locale = Globalization.CultureInfo.InvariantCulture
        }
        dataAdapter.Fill(table)
        bindingSource1.DataSource = table

        ' Resize the DataGridView columns to fit the newly loaded content.
        dataGridView1.AutoResizeColumns(
            DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader)

    Catch ex As SqlException
        MessageBox.Show("To run this example, replace the value of the " +
            "connectionString variable with a connection string that is " +
            "valid for your system.")
    End Try
End Sub

Reference: Bind data to DataGridView Control - Windows Forms | Microsoft Docs[^]
 
Share this answer
 
Comments
prafulla1978 12-Aug-20 4:58am    
This is not right answer because i have given allready header names in datagridviewrow to headers by edit columns option on right click . this method shows next all columns with data but seperate columns in grid.
Sandeep Mewara 12-Aug-20 5:06am    
Turn off autogenerate columns to false to avoid adding columns during bind.

Make sure the columns definded by you has the fields mapped to what is there in data source.
Sandeep Mewara 12-Aug-20 5:07am    
Reference: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.autogeneratecolumns?view=netcore-3.1
Or ... just use the DataTable you already have:
VB
DgTotalInputDataSource = dt;


But ... if it's taking a really long time, it's also likely that you have far too much data. You should never show more than around 100 to 200 rows to a user: If you have more it becomes increasingly hard to find the data they are interested in. Instead, page the data, provide filters and searches, make the user's life easier.
 
Share this answer
 
v2
Comments
prafulla1978 12-Aug-20 5:00am    
This is not succesfull beacouse after adding filter it not saves new row data/ insert new row data in database
prafulla1978 20-Sep-20 11:18am    
how to call getdata method on button click

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900