Compartilhar via


DataGridView Control Sample

This sample demonstrates several features of the DataGridView control in a master/detail scenario. The sample shows a parent table in the main form and provides access to related child tables through a link column. When the user clicks the link for a particular row, the related child table is shown in a separate window.

For information about using the samples, see the following topics:

Security noteSecurity Note

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To build and run the sample using the command prompt

  1. At the command prompt, navigate to the location where you saved the uncompressed sample files.

  2. Navigate to one of the language-specific subdirectories under the DataGridViewSample directory.

  3. Type msbuild to build the sample using the solution (.sln) file provided.

  4. Type cd DataGridView\bin\Debug to navigate to the location of the executable file.

  5. Type DataGridView.exe to run the sample.

To build and run the sample using Visual Studio

  1. In Windows Explorer, navigate to the location where you saved the uncompressed sample files.

  2. Navigate to one of the language-specific subdirectories under the DataGridViewSample directory.

  3. Double-click the solution (.sln) file to open it in Visual Studio.

  4. In Visual Studio, press F5 to build and run the sample.

To use the DataGridView Sample

  1. Click the View Orders link for the row with the ID of "BERGS".

    In the child window, red cell backgrounds indicate orders that were shipped after their required date, and yellow backgrounds indicate orders that were shipped in the week before the required date. Additionally, the row headers display row numbers.

  2. Scroll to the right to view additional information.

    Observe that the check boxes remain visible along with the row headers.

  3. Select a few check boxes, and then sort the control by another column.

    Observe that the check boxes maintain their selected state.

Demonstrates

  • Binding the DataGridView control to a database table.

  • Using a DataGridViewLinkColumn to display related records using a separate window.

  • Using cell formatting to highlight cells with important values.

  • Using custom row painting to show row numbers in row headers.

  • Adding an unbound DataGridViewCheckBoxColumn that remains frozen in place while other columns scroll.

  • Using virtual mode to maintain the values in the unbound column when sorting.

See Also

Tasks

How to: Bind Data to the Windows Forms DataGridView Control
How to: Customize Data Formatting in the Windows Forms DataGridView Control
How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control
How to: Implement Virtual Mode in the Windows Forms DataGridView Control

Reference

DataGridView
DataGridViewLinkColumn
DataGridViewCheckBoxColumn

Other Resources

Windows Forms Controls Samples