Using MSFlexgrid with a combo box in C#






4.60/5 (4 votes)
How to edit MSFlexgrid with a combo box in C#.
Introduction
I wrote an article before about an ActiveX control using MSFlexGrid
to edit cells and include a combo box to the ActiveX control. When I create the ActiveX control to use with VB6, I can view the combo box at any column. To see this ActiveX control and its demo application, click here. But, when I create the ActiveX control to use with C#, I can include the combo box to one column only; you can see this article here.
Now, I created a new ActiveX to edit the cells of MSFlexGrid
and to include a combo box to more than one column for use with C#. With my ActiveX control, you can:
- Edit any cell in the flex grid.
- Delete any row.
- Delete all rows and begin with one row.
- Add a combo box to any column and use more than one.
- Write data to any cell.
- Read data from any cell.
- Read all data from the grid.
Background
When using my ActiveX control (MKGrid
), the flex grid begins with one row like a DataGrid
control, then increases rows one by one after editing the previous row. My ActiveX control has the following methods and properties:
Method/Property | Definition | Example |
---|---|---|
|
Get the text of the cell |
|
setCellText(int row, int col, string textMatrix) |
Set the string |
|
|
Set |
|
|
Align |
|
|
Align |
|
|
Align |
|
|
Align |
|
|
Align |
|
|
Align |
|
|
Let the cell in |
|
|
Add item to the combo box in |
|
|
Delete all items from combo box in |
|
|
Get number of items of combo box in |
|
|
Delete a row from the grid. |
|
|
Clear the grid. |
|
|
Set |
|
|
Get/set index of row. |
|
|
Get/set index of column. |
|
|
Get/set number of rows. |
|
|
Get/set number of columns |
|
|
Get/set number of fixed rows. |
|
|
Get/set number of fixed columns. |
|
|
Get/set index of selected row. |
|
|
Get/set index of selected column. |
|
To use my ActiveX (MKGrid
), you must add MKFlexgrid.dll to the References. To add the control to your own form, you have to import it to the toolbox by browsing and adding MKFlexgrid.dll. When you get it on your toolbox, just drag it and drop it on your form. You must add the following files also to References:
- AxInterop.MSFlexGridLib.dll
- Interop.MSFlexGridLib.dll
Using the Code
Setting the columns:
//
// Set number of columns:
//
mkGrid1.SolidCols = 0; // if you not need fixed columns
mkGrid1.AllCols = 5; // number of columns
//
// Set width of column:
//
mkGrid1.Colwidth(1, 150); // width in pixles
//
// Set alignment of column (You can change many using loop)
//
mkGrid1.FixedAlignCenter(1); // alignment of fixed row, column #1 (to Center)
mkGrid1.ColAlignLeft(1); // alignment column #1 (to Left)
Setting the combo boxes:
//
// Set combo box at any column:
//
mkGrid1.CellType(3, true); // combo box at column #3
mkGrid1.ComboClear(3); // clear this combo
mkGrid1.ComboAddItem(3, „Visual Studio“); // add item to this combo
Setting the cells:
//
// Send data to any cell:
//
mkGrid1.setCellText(3, 2 „Books“);
//
// Read any cell:
//
string strCell = mkGrid1.getCellText(3, 2);
You can go back to the source files of the project (TestMKGrid) to see more examples.
Remarks
When extracting MKFlexgrid.zip, you can find the file: ..\MKFlexgrid\ActiveXcontrol\MKFlexgrid.dll.
Find the project to test the ActiveX control in the folder: ..\MKFlexgrid.
This project has three forms:
Form1
: to choose language.Form2
: for English.Form3
: for Arabic.
Form2
and Form3
have the same controls:
- The ActiveX control (
mkGrid1
) you get when adding the file MKFlexgrid.dll to References. - The button control (
btnDelRow
) to delete a row. - The button control (
btnDelAll
) to clear the grid. - The button control (
btnWriteCell
) to write data to the first cell. - The button control (
btnReadOne
) to read the current cell. - The button control (
btnReadAll
) to copy all data from the grid to the list box. - The button control (
btnExit
) to close the form. - The list box control (
lstAllData
) that holds data from the grid.
Last Words
I hope this article is useful and helps you to create your applications. If you have any new ideas or if you find any problems, please tell me about it. Just remember to put it in simple English because my English is poor! Thanks to CodeProject and thanks to all.
I hope my article is useful, thanks to all my friends.