Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Using MSFlexgrid with a combo box in C#

4.60/5 (4 votes)
10 Dec 2007CPOL4 min read 1   1.9K  
How to edit MSFlexgrid with a combo box in C#.

Screenshot -

(Form1)

Screenshot - img11.JPG

(Form2)

Screenshot - img12.JPG

(Form3)

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/PropertyDefinitionExample

getCellText(int row, int col)

Get the text of the cell (row, col).

string strCell = mkGrid1.getCellText(2, 5);

setCellText(int row, int col, string textMatrix)

Set the string textMatrix in the cell (row, col).

mkGrid1.setCellText(2, 4, “Visual Studio”);

ColWidth(int index, int colWidth)

Set Col(index) with a width colWidth.

mkGrid1.ColWidth(0, 150);

ColAlignLeft(int index)

Align Col(index) to left.

mkGrid1.ColAlignLeft(2);

ColAlignRight(int index)

Align Col(index) to right.

mkGrid1.ColAlignRight(2);

ColAlignCenter(int index)

Align Col(index) to center.

mkGrid1.ColAlignCenter(2);

FixedAlignLeft(int index)

Align FixedCol(index) to left.

mkGrid1.FixedAlignLeft(1);

FixedAlignRight(int index)

Align FixedCol (index) to right.

mkGrid1.FixedAlignRight(1);

FixedAlignCenter(int index)

Align FixedCol (index) to center.

mkGrid1.FixedAlignCenter(1);

CellType(int index, bool IsCombo)

Let the cell in Col(index) include a combo box if IsCombo = true.

mkGrid1.CellType(2, true);

ComboAddItem(int index, string Item)

Add item to the combo box in Col(index).

mkGrid1.ComboAddItem(2, "Visual Studio");

ComboClear(int index)

Delete all items from combo box in Col(index).

mkGrid1.ComboClear(2);

ComboListCount(int index)

Get number of items of combo box in Col(index).

int i = mkGrid1.ComboListCount(2);

DelRow()

Delete a row from the grid.

mkGrid1.DelRow();

DelAll()

Clear the grid.

mkGrid1.DelAll();

RightDirection

Set RightToLeft of the control (true / false).

mkGrid1.RightDirection = true; //for Arabic language

AnyRow

Get/set index of row.

int r = mkGrid1.AnyRow;

AnyCol

Get/set index of column.

int c = mkGrid1.AnyCol;

AllRows

Get/set number of rows.

int n = mkGrid1.AllRows;

AllCols

Get/set number of columns

int n = mkGrid1.AllCols;

SolidRows

Get/set number of fixed rows.

mkGrid1.SolidRows = 1;

SolidCols

Get/set number of fixed columns.

mkGrid1.SolidCols = 0;

RowSelect

Get/set index of selected row.

int r = mkGrid1.RowSelect;

ColSelect

Get/set index of selected column.

int c = mkGrid1.ColSelect;

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:

C#
//
// 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:

C#
//
// 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:

C#
//
// 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)