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

Using Grid with CheckBox and ComboBox as COM Components in VB.NET and C#

4.13/5 (5 votes)
28 Mar 2010CPOL5 min read 1   5.8K  
Using ActiveX Grid control with CheckBox and ComboBox in VB6, VB.NET and C#
image0108.JPG

Introduction

I made some attempts to make editing the Grid control easy, now this is my new attempt to create an ActiveX control. I give the name (MKGrid) to my new ActiveX. With my ActiveX control, you can:

  • Bind to database file
  • Add, Edit and delete records
  • Use Combo box, Date picker and Check box at any column
  • Add row, delete row, insert row to unbound grid
  • Hide column, lock column, resize column and sort column
  • Save unbound grid after edit it as *.mdb file or *.htm file
  • After editing cell and leave it, the cell will be saved
  • Have more properties and methods
  • Use my AcriveX MKGrid in C# and VB.NET with differences in the names of some properties and its parameters

Background

When using my ActiveX control (MKGrid), the grid begins with one row as Data grid control then increase rows one by one after editing the current row.
When you bind the grid with database file, my ActiveX will look for the Date field, if any then put Date picker at the Date column, also my ActiveX will look for the Boolean field (Yes, No), if any then put Check box at the Boolean column without user intervention. This means that the user does not need to specify the type field in this case.

In the following tables, you can read about some methods and some properties as examples.

In C#

Method/Property Explanation Example
ComboItemsList (ComboCol, ComboList)Set the List of items [ComboList] to the Combo box at the Column #ComboCol.int c = 4;<br />string strList= "Apple/Banana/Orange/Melon/<br />Peach/Strawberry";<br />MKGrid.ComboItemsList(ref c, ref strList);
DeleteRecordDeletes the current Record from database file.MKGrid.DeleteRecord;
InsertRowInsert new Row to unbound Grid.MKGrid.InsertRow;
set_ColumnHidden(number,ColType)Returns or sets a value indicating whether a specified column is hide.MKGrid.set_ColumnHidden(4, false);
set_ColumnType (number, ColType)Returns or sets the type of control to show in a column.MKGrid.set_ColumnType (4, KGrid.grdColumnType.colCheckBox)
LoadGridByQuery( MyDataFile, GridSQL)Populate the grid with query [GridSQL] from the file [MyDataFile].string MyDataFile = <br />Application.StartupPath + @"\DataFiles\" + "test.mdb";<br />string MySql = "SELECT * FROM Customers"<br />MKGrid.LoadGridByQuery(ref MyDataFile, ref MyTable);
LoadGridFromTable( mdbDataFile, mdbTable)Bind the Grid to the Table [mdbTable] from the database file [mdbDataFile].string MyDataFile = Application.StartupPath <br />+ @"\DataFiles\" + "test.mdb";<br />string MyTable = "Customers";<br />MKGrid.LoadGridFromTable<br />(ref MyDataFile, ref MyTable);

I created a project to test my control, I wrote its code using C# (2003).
You can go back to the source files to read more than the previous examples.

In VB.NET

Method/Property Explanation Example
ComboItemsList (ComboCol, ComboList)Set the List of items [ComboList] to the Combo box at the Column #ComboCol.MKGrid.ComboItemsList (4, "Apple/Banana/Orange/Melon/<br />Peach/Strawberry")
DeleteRecordDeletes the current Record from database file.MKGrid.DeleteRecord
InsertRowInsert new Row to unbound Grid.MKGrid.InsertRow
ColumnHidden(number,ColType)Returns or sets a value indicating whether a specified column is hide.MKGrid.ColumnHidden(5, True)
set_ColumnType (number, ColType)Returns or sets the type of control to show in a column.MKGrid.set_ColumnType (4, KGrid.grdColumnType.colCheckBox)
LoadGridByQuery( MyDataFile, GridSQL)Populate the grid with query [GridSQL] from the file [MyDataFile].Dim MyDataFile As String = Application.StartupPath & "\" & "Test.mdb"<br />Dim MySql As String = "SELECT * FROM Customers"<br />MKGrid.LoadGridByQuery(MyDataFile, MySql)
LoadGridFromTable( mdbDataFile, mdbTable)Bind the Grid to the Table [mdbTable] from the database file [mdbDataFile].Dim MyDataFile As String = <br />StartupPath & "\" & "Test.mdb"<br />Dim MyTable As String = "Customers"<br />MKGrid.LoadGridFromTable (MyDataFile, MyTable)

I create a project to test my control, I write its code under VisualBasic.NET (2003).
You can go back to the source files to read more than the previous examples.

In VB6

Method/Property Explanation Example
ComboItemsList ComboCol, ComboListSet the List of items [ComboList] to the Combo box at the Column #ComboCol.MKGrid1.ComboItemsList 4, "Apple/Banana/Orange/Melon/<br />Peach/Strawberry"
DeleteRecordDeletes the current Record from database file.MKGrid1.DeleteRecord
InsertRowInsert new Row to unbound Grid.MKGrid1.InsertRow
ColumnHidden(number) [=Boolean]Returns or sets a value indicating whether a specified column is hide.MKGrid1.ColumnHidden(5) = True
ColumnType(number) [=ColType]Returns or sets the type of control to show in a column.MKGrid1.ColumnType(4) = colCheckBox
LoadGridByQuery MyDataFile, GridSQLPopulate the grid with query [GridSQL] from the file [MyDataFile].MyDataFile = App.Path & <br />"\" & "Test.mdb"<br />MySql = "SELECT * FROM Customers"<br />MKGrid1.LoadGridByQuery MyDataFile, MySql
LoadGridFromTable mdbDataFile, mdbTableBind the Grid to the Table [mdbTable] from the database file [mdbDataFile].MyDataFile = App.Path <br />& "\" & "Test.mdb"<br />MyTable = "Customers"<br />MKGrid1.LoadGridFromTable MyDataFile, MyTable

I created a project to test my control, I wrote its code using Visual Basic 6.
You can go back to the source files to read more than the previous examples.

Using the Code

About columns:

C#
// C#:
MKGrid.Rows = 2; // number of rows
MKGrid.Cols = 6; // number of columns

// Set Header of column:
// 
MKGrid.set_ColHeaderText(1, "Emp ID");  
MKGrid.set_ColHeaderText(2, "Emp Name");
MKGrid.set_ColHeaderText(3, "B_Date");
MKGrid.set_ColHeaderText(4, "Salary");
MKGrid.set_ColHeaderText(5, "Married");
MKGrid.set_ColHeaderText(6, "City");
C#
'VB.NET:
MKGrid1.Rows = 2 ' number of rows
MKGrid1.Cols = 6 ' number of columns

' Set Header of column:
' 
MKGrid1.set_ColHeaderText(1, "Emp ID")
MKGrid1.set_ColHeaderText(2, "Emp Name")
MKGrid1.set_ColHeaderText(3, "B_Date")
MKGrid1.set_ColHeaderText(4, "Salary")
MKGrid1.set_ColHeaderText(5, "Married")
MKGrid1.set_ColHeaderText(6, "City")
VB.NET
'VB6:
MKGrid1.Rows = 2 ' number of rows
MKGrid1.Cols = 6 ' number of columns

' Set Header of column:
' 
MKGrid1.ColHeaderText(1) = "Emp ID"
MKGrid1.ColHeaderText(2) = "Emp Name"
MKGrid1.ColHeaderText(3) = "B_Date"
MKGrid1.ColHeaderText(4) = "Salary"
MKGrid1.ColHeaderText(5) = "Married"
MKGrid1.ColHeaderText(6) = "City"

About combo box:

C#
//C#:// items list for Combo box.
//// we are using the slash "/" as delimiter between items.int c = 6;
//string strList = "Ankara/Berlin/Cairo/London/Los Angeles/Madrid/Marseille/";
// fill CombBox at column #6 with previous list:
//MKGrid.ComboItemsList (ref c, ref strList);
VB.NET
'VB.NET:Dim strList String = "Ankara/Berlin/Cairo/London/Los Angeles/Madrid/Marseille/"'_
 fill CombBox at column #6 with previous list:MKGrid1.ComboItemsList (6, strList)As 
VB.NET
'VB6:Dim strList String strList = _
"Ankara/Berlin/Cairo/London/Los Angeles/Madrid/Marseille/"' fill CombBox at _
column #6 with previous list:MKGrid1.ComboItemsList (6, strList)
 As 

About Date Picker and Check box:

C#
//C#:// Set Date Picker and Check box:MKGrid.set_ColumnType(3, 
//KGrid.grdColumnType.colDateControl); Date control at column #3
//MKGrid.set_ColumnType(5, KGrid.grdColumnType.
// colCheckBox ); 
// CheckBox at column #5
VB.NET
'VB.NET:MKGrid1.set_ColumnType(3, KGrid.grdColumnType.colDateControl) 
' Date control at column #3MKGrid1.set_ColumnType(5, KGrid.grdColumnType.colCheckBox) 
' CheckBox at column #5'VB6:MKGrid1.ColumnType(3) = colDateControl 
' Date control at column #3MKGrid1.ColumnType(5) = colCheckBox 
' CheckBox at column #5 

Remarks

  • When extracting the *.zip files, you can find:
  • The ActiveX control KGrid.ocx
  • C# Project to test ActiveX
  • VB.NET Project to test ActiveX
  • VB6 Project to test ActiveX
  • Test_EN.mdb, Test_Ar.mdb and MyMarket.mdb
  • MKGrid help file
  • Set my ActiveX "KGrid.ocx" in your program directory, but it is better if you set it in the directory:
    "c:\windows\system32\" then register the ActiveX control by running the following line:
    regsvr32 c:\windows\system32\KGrid.ocx
  • To add my ActiveX "MKGrid" control to your form:
    • Open Components dialog, then click Browse
    • From its directory or c:\windows\system32\, you can add the ActiveX
  • You can refer to the help file for all methods and properties. If there are any problems, please let me know.

Last Words

I hope this article is useful and helps you to create your applications. If you have any ideas, please tell me. Thanks to CodeProject and thank you all.

Mostafa Kaisoun
m_kaisoun@hotmail.com

License

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