How to Update and Refresh the DataGridView

How to Update and Refresh the DataGridView thumbnail
Update and Refresh the DataGridView

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.

Instructions

    • 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

      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 adapter = new OleDbDataAdapter(query, con);

      OleDbCommandBuilder build = new OleDbCommandBuilder(adapter);

      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

      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();

      dAdapter.Fill(table);

    • 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

      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.

Related Searches:

References

Resources

  • Photo Credit Jupiterimages/Photos.com/Getty Images

Comments

You May Also Like

Related Ads

Featured