Creating a DataGrid

The Visual Studio .NET development environment provides a very powerful control for displaying tabular data: the DataGrid. You can display a great deal of information from a table just by binding the table to the DataGrid.

Create a new C# or VB.NET project called SimpleADODataGrid. Drag a DataGrid control onto the form. Visual Studio will name it DataGrid1. Rename the DataGrid dgBugs and widen the grid to the width of the form.

In the constructor, retrieve the Bugs table from the database, much as you did in Example 19-2, except that this time you'll modify the select statement to retrieve all the fields from the bugs table:

image with no caption

string connectionString =
   "server=YourServer; uid=sa; 
   pwd=YourPassword; database=WindForms_Bugs";
   
            // get records from the Bugs table
            string commandString = 
               "Select * from Bugs";
   
// create the data set command object 
// and the DataSet
SqlDataAdapter dataAdapter = 
   new SqlDataAdapter(
   commandString, connectionString);
   
DataSet DataSet = new DataSet(  );
   
// fill the data set object
dataAdapter.Fill(DataSet,"Bugs");
   
// Get the one table from the DataSet
DataTable dataTable = DataSet.Tables[0];

image with no caption

Public Sub New( ) MyBase.New( ) 'This call is required by the Windows Form Designer. InitializeComponent( ) Dim connectionString As String connectionString ...

Get Programming .NET Windows Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.