Document directory
- Step 1
- Step 2
- Step 3
- Step 4
- Step 5
- Step 6
- Step 7
- Step 8
Introduction
When I had to use the FlexGrid for the first time, I was too frustrated to find out that no proper documentation existed on the net. the ones available were too complicated for me to understand. and the ones that were easy were in VB. this article is to help persons who are going to use the FlexGrid in their applications using VC ++. also take a look at the warning note at the end of this article.
Implementationstep 1
First, include the msflexgrid control in the project. To do that :-
- In the VC ++ ide menu, go to project-> Add to project-> components and controls menu.
- Then, in the dialog box that appears, select registered ActiveX controls. There, you can see a control named "Microsoft Hierarchical FlexGrid Control ...".
- Select that and add it to your project using the insert button. You can now see the FlexGrid Control in the resource editor's Controls Toolbar.
- Add it to your dialog as you add a control usually.
Step 2
Then, set the properties as you want it to be. Right click on the grid and select the properties option. Change the settings there as per your requirements.
Step 3
Add a member variable for that grid. E. g.m_Grid
. The sample given below is for an example. Use it as a guideline to make your application.
Step 4
Its better to clear the FlexGrid before you do any operations on it. To do that, use :-
m_Grid.Clear();
Step 5
To add records to the grid, use something like this.
CString strName,strRemarks;m_nCount = 0;// Clear and refresh the grid m_Grid.Clear();m_Grid.Refresh();// Get the value for strName from the database here // Get the strRemarks from the database herem_nCols = m_Grid.GetCols();m_Grid.SetTextArray(0, "Name " ); // First Columnm_Grid.SetTextArray(1, "Remarks " ); // Second Columnm_nCount++;// Fill First Column m_Grid.SetTextArray( m_nCols * m_nCount + 0,strName ); // Fill Second Columnm_Grid.SetTextArray( m_nCols * m_nCount + 1, strRemarks ); // Redraw the gridm_Grid.SetRedraw(TRUE);m_Grid.Refresh();
Step 6
To retrieve data from the grid when the user double clicks on a record, add a double click message handler for the grid using the Class Wizard. Assuming that you have named the functionOnDblClickGrid
, Then :-
void YourDialog::OnDblClickGrid(){ // Get the current row and column int nRow = m_Grid.GetRow(); int nCol = m_Grid.GetCol(); CString strName,strRemarks; // Get data from the First Column strName = m_Grid.GetTextMatrix(nRow,nCol+0); // Get data from the Second Column strRemarks = m_Grid.GetTextMatrix(nRow,nCol+1); }
Step 7
To move to the previous record, do this :-
Collapse
void YourDialog::OnBtnPrevious(){ m_Grid.SetRedraw(FALSE); // Get the current selection int NextRow = m_Grid.GetRowSel(); // If the position is at the last record, return if(NextRow <= 1) { return; } else { long BackColor[2],FontColor[2]; int Column; // The BackColor and the FontColor variables // are manipulated because we want a // selected effect to be given to the previous // record. Here, we are merely changing // the color of the selected // row to give it that effect. BackColor[0] = 0x00FFFFFF; BackColor[1] = 0x00FFFFB0; FontColor[0] = 0x00400000; FontColor[1] = 0x000000FF; for(Column = 1; Column < m_Grid.GetCols(); Column++) { m_Grid.SetCol(Column); m_Grid.SetCellBackColor(BackColor[0]); m_Grid.SetCellForeColor(FontColor[0]); } m_Grid.SetRow(--NextRow); for(Column = 1; Column < m_Grid.GetCols(); Column++) { m_Grid.SetCol(Column); m_Grid.SetCellBackColor(BackColor[1]); m_Grid.SetCellForeColor(FontColor[1]); } m_Grid.Refresh(); m_Grid.SetRedraw(TRUE); }}
Step 8
To got to the next record, do the reverse of the code above. Insteadm_Grid.SetRow(--NextRow)
It wocould bem_Grid.SetRow(++NextRow)
.
Warning
Whenever you use a FlexGrid in your application, you have to be twice as careful. be sure to take regular backups. sometimes, you might note that your compilation time is too longer than usual. it wocould be more and more slow with each build. if you feel so, check the size of your. RC file. if its size is abnormal (I cant specify a size because it depends on your project and its resource contents), then delete the FlexGrid from your application, close Visual Studio and copy. RC file from your backup. open your project again. that shoshould solve your problem. i'm not sure why this happens, maybe its a bug in FlexGrid that extends upts or damages it. but from your side, be careful.
Conclusion
The FlexGrid is a very easy control to use. But whenever you use it, repeat the mantra"Take backups of Your. RC file regularly"Until it becomes a habit. That's it. All luck and have a great time.