How to Update and Refresh the DataGridView

Written by jim campbell
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Update and Refresh the DataGridView
(Jupiterimages/ Images)

The DataGridView is a Microsoft .NET control you place on your Web page to display several records from a database. You update the DataGridView by binding it to a data table. When you bind the control to a data table, it automatically refreshes and displays the new set of records to the user. You must create the data for the DataGridView, which means you pull records from the database and store them in the data table.

Skill level:


  1. 1

    Double-click your Windows form file on your computer. A Visual Studio project file that contains a DataGridView has the extension "SLN." Double-click this file to open your project.

  2. 2

    Create your database connection. Your DataGridView is filled with table information, so the database connection is first made using the username and password. The following shows you how to make a database connection:

    string con = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\myDatabase.mdb";

    string query = "SELECT * FROM customers";

    OleDbDataAdapter adaptor = new OleDbDataAdapter(query, con);

    OleDbCommandBuilder build = new OleDbCommandBuilder(adaptor);

    In this example, a connection is made to the "myDatabase.mdb" Access file. Replace this with your own Access file or your database server.

  3. 3

    Create and fill the data table. Step 1 retrieved the data for your DataGridView. You must then create a data table to file the grid with records. The code below shows you how to create a table and fill it with your records:

    DataTable table = new DataTable();


  4. 4

    Set the data table to the DataGridView's "DataSource" property. This property sets the grid's list of data. The following code creates a DataGridView and sets the grid's data to the newly created data table:

    DataGridView dv = new DataGridView();

    BindingSource binding = new BindingSource();

    binding.DataSource = table;

    dv.DataSource = binding;

  5. 5

    Click the Visual Studio "Save" button to save your code. To test the new DataViewGrid, click the "Run" button to compile the code and show the grid on your desktop.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.