Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
MSDN Library

DataGridView Control Sample

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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.

The following illustration shows the main form:

DataGridView Sample Main Form

The following illustration shows the detail form that appears when the user clicks the View Orders link in the Orders column:

DataGridView Sample Detail Form

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

Security note Security 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.

  • 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.

Show:
© 2015 Microsoft