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:
Other People Are Reading
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.
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.
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();
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;
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.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for