How to Change the DataGridView Cell Backcolor

How to Change the DataGridView Cell Backcolor thumbnail
The backcolor of a cell is its background color.

The Microsoft Visual Studio development environment contains a graphical editor that allows you to build applications by dragging and dropping items onto a form. This reduces the development time for applications that use graphical user interfaces. However, some graphical items have features that can only be accessed by writing code. For example, changing the background color of a cell in a DataGridView requires you to write a line of code. The DataGridView object is a method of displaying data in table format with columns, rows and cells.

Instructions

    • 1

      Open Visual Studio 2010 by clicking on its program icon. When it loads, click "File," "New," then "Project" and then click "Visual C#/Windows Forms Application." A new Windows Form Application project will be created and a blank Windows Form will appear in the main editor window.

    • 2

      Click and drag the "DataGridView" from the "Toolbox" panel over to the Windows Form. Release the mouse button to place the table on the form.

    • 3

      Click the tiny black arrow located in the upper-right corner of the "DataGridView." A menu will appear. Click "Add Column" and click the "OK" button when the "Add Column" dialog box appears.

    • 4

      Click the "Cancel" button in the "Add Column" dialog box to close it. Your "DataGridView" should now have one column.

    • 5

      Click "View," which is a menu item located on the top row of buttons on the Visual Studio window. A menu will appear. Click "Code" to switch from the graphical editor to a source code editor. A source code file will appear.

    • 6

      Locate the following text in the source code file: InitializeComponent();

    • 7

      Write the following statement immediately after the "InitializeComponent();" line to set the cell background color to black: dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Black;

    • 8

      Click the green "Play" button located in the top row of buttons in the Visual Studio window. A Windows Form will appear and displays a single "DataGridView" with one column and one row. The cell at the intersection between the row and column will have a black background color.

Tips & Warnings

  • The "Toolbox" panel is usually to the right of the Windows Form. This isn't always the case, as panels can be moved around and their configuration is saved when you restart Visual Studio. The "Toolbox" panel contains a list of graphical user interface items that can be dragged onto your Windows Form to create an application.

Related Searches:

References

  • Photo Credit Brand X Pictures/Brand X Pictures/Getty Images

Comments

Related Ads

Featured