Grid Control Showing Associations

What it is:

This Grid Control (CGridCtrl) allows you to display a 2-Dimensional grid with check marks in it. It can be used to show associations of the two columns. For example the Screen Shot is a grid of NFL football teams. Each check mark represents a game which will be played. In the example app, you can change the font style and size by clicking on the Change Font button. You can add new rows and columns by inputting a name into the Name edit box and pressing the Add Row or Add Column buttons. Selecting the OK button will save the grid to a file to be loaded on the next session. Click the Cancel button to exit without saving. There is also functionality in the class to remove rows and columns, but the example application does not demonstrate it.

The Idea:

We wanted a grid which could display an ON/OFF state of two factors. In our case if a user was a member of a certain security group. The problem was that a normal grid got unnecessarily wide because there were so many columns. The idea behind this grid is to show several columns by slanting the column header text to 45 degrees. The grid control also supports Tool Tips and Serialization. So you can store the grid to a file and restore it in a later session.

How To Use:

Using the CGridCtrl class is simple. First you will need to add the CGridCtrl.h (cpp) files to your project. You might want to rebuild your class wizard database so that it knows about the new classes in your project. Add the window to your dialog by using the resource editor. Using class wizard, add a member variable for the window using the CGridCtrl class as the variable type. In the dialog's OnInitDialog method, add a calls to the control's AddColumn, AddRow and SetCheck methods to create the grid. You can also call a Serialize method to create the grid from a file. Take a look at the example application's code to see how this is done.

BOOL CGridTestDlg::OnInitDialog() 

    // Attempt to load the grid from a previously saved File. 
    CFile fileArchive; 
    if (fileArchive.Open("GridTest.dat", CFile::modeRead)) 
        CArchive archive(&fileArchive, CArchive::load); 

        // Serialize the window state object 
    else // The file did not exist.  Do the default thing. 
        // Add all of the rows. 
        m_wndGrid.AddRow("Broncos", FALSE); 
        m_wndGrid.AddRow("Packers", FALSE); 
        m_wndGrid.AddRow("Dolphins", FALSE); 
        m_wndGrid.AddRow("Niners", FALSE); 
        m_wndGrid.AddRow("Cowboys", FALSE); 
        m_wndGrid.AddRow("Jets", FALSE); 
        m_wndGrid.AddRow("Redskins", FALSE); 
        m_wndGrid.AddRow("Colts", FALSE); 
        m_wndGrid.AddRow("Bucaneers", FALSE); 
        m_wndGrid.AddRow("Chiefs", FALSE); 
        m_wndGrid.AddRow("Raiders", FALSE); 

        // Add all of the columns 
        m_wndGrid.AddColumn("Broncos", FALSE); 
        m_wndGrid.AddColumn("Packers", FALSE); 
        m_wndGrid.AddColumn("Dolphins", FALSE); 
        m_wndGrid.AddColumn("Niners", FALSE); 
        m_wndGrid.AddColumn("Cowboys", FALSE); 
        m_wndGrid.AddColumn("Jets", FALSE); 
        m_wndGrid.AddColumn("Redskins", FALSE); 
        m_wndGrid.AddColumn("Colts", FALSE); 
        m_wndGrid.AddColumn("Bucaneers", FALSE); 
        m_wndGrid.AddColumn("Chiefs", FALSE); 
        m_wndGrid.AddColumn("Raiders", TRUE); 

        // NOTE: the final call to create the last column passes 
        // a TRUE value to initialize the grid and prepare it for 
        // drawing. 

        // Set the check marks passing in the row and column. 
        m_wndGrid.ToggleCheck(0, 1); 
        m_wndGrid.ToggleCheck(1, 0); 
        m_wndGrid.ToggleCheck(2, 3); 

    return TRUE;  // return TRUE  unless you set the focus to a control 

CGridCtrl Manipulation (Public Methods):

virtual void Serialize(CArchive& ar)
Used to store or load a grid to/from a data file.

void Initialize()
Used to Initialize all of the drawing data used to draw the control.
(Must be done before drawing the control)

void AddColumn(CString strColumn, BOOL bReInitialize = TRUE)
Adds a new column to the grid. Reinitializes the drawing parameters if flag is set.

void AddRow(CString strRow, BOOL bReInitialize = TRUE)
Adds a new row to the grid. Reinitializes the drawing parameters if flag is set.

BOOL RenameColumn(int nIndex, CString strColumn)
Renames the column at the given index to the given string.

BOOL RenameColumn(CString strOld, CString strNew)
Renames the column with the given name to the given string.

BOOL RenameRow(int nIndex, CString strRow)
Renames the row at the given index to the given string.

BOOL RenameRow(CString strOld, CString strNew)
Renames the row with the given name to the given string.

BOOL DeleteColumn(int nIndex)
Removes the column at the given index from the grid.

BOOL DeleteColumn(CString strColumn)
Removes the column with the given name from the grid.

BOOL DeleteRow(int nIndex)
Removes the row at the given index from the grid.

BOOL DeleteRow(CString strRow)
Removes the row with the given name from the grid.

void ToggleCheck(int nColumn, int nRow)
Toggle the check mark on and off at the given column and row indexes.

BOOL IsChecked(int nColumn, int nRow)
Returns TRUE if the given column and row are checked. FALSE otherwise.

int GetNumColumns()
Returns the number of columns in the grid.

int GetNumRows()
Returns the number of rows in the grid.

void SetFontStyle(CString strFont)
Sets a new font style for text in the grid. Redraws the control.

void SetFontSize(int nHeight)
Sets a new font size for text in the grid. Redraws the control.

int GetFontSize()
Returns the size of the font used for drawing the grid.

CString GetFontStyle()
Returns the face name of the font used for drawing the grid.

Download source and demo application - 31 KB .


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds