Click here to Skip to main content
Click here to Skip to main content

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

By , 28 Mar 2010
Rate this:
Please Sign up or sign in to vote.
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;
string strList= "Apple/Banana/Orange/Melon/
Peach/Strawberry";
MKGrid.ComboItemsList(ref c, ref strList);
DeleteRecord Deletes the current Record from database file. MKGrid.DeleteRecord;
InsertRow Insert 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 = 
Application.StartupPath + @"\DataFiles\" + "test.mdb";
string MySql = "SELECT * FROM Customers"
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 
+ @"\DataFiles\" + "test.mdb";
string MyTable = "Customers";
MKGrid.LoadGridFromTable
(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/
Peach/Strawberry")
DeleteRecord Deletes the current Record from database file. MKGrid.DeleteRecord
InsertRow Insert 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"
Dim MySql As String = "SELECT * FROM Customers"
MKGrid.LoadGridByQuery(MyDataFile, MySql)
LoadGridFromTable( mdbDataFile, mdbTable) Bind the Grid to the Table [mdbTable] from the database file [mdbDataFile].
Dim MyDataFile As String = 
StartupPath & "\" & "Test.mdb"
Dim MyTable As String = "Customers"
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, ComboList Set the List of items [ComboList] to the Combo box at the Column #ComboCol.
MKGrid1.ComboItemsList 4, "Apple/Banana/Orange/Melon/
Peach/Strawberry"
DeleteRecord Deletes the current Record from database file. MKGrid1.DeleteRecord
InsertRow Insert 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, GridSQL Populate the grid with query [GridSQL] from the file [MyDataFile].
MyDataFile = App.Path & 
"\" & "Test.mdb"
MySql = "SELECT * FROM Customers"
MKGrid1.LoadGridByQuery MyDataFile, MySql
LoadGridFromTable mdbDataFile, mdbTable Bind the Grid to the Table [mdbTable] from the database file [mdbDataFile].
MyDataFile = App.Path 
& "\" & "Test.mdb"
MyTable = "Customers"
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#:
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");
'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")
'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#:// 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:Dim strList String = "Ankara/Berlin/Cairo/London/Los Angeles/Madrid/Marseille/"'_
 fill CombBox at column #6 with previous list:MKGrid1.ComboItemsList (6, strList)As 
'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#:// 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: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)

About the Author

No Biography provided

Comments and Discussions

 
QuestionAre all properties and event procedures for traditional VB6 FlexGrid works in MKGrid PinmemberMember 82261899-Sep-11 17:28 
AnswerRe: Are all properties and event procedures for traditional VB6 FlexGrid works in MKGrid PinmemberMostafa Kaisoun10-Sep-11 12:36 
Questionhow can i get the value of a checkbox [modified] Pinmemberjorge12222-Aug-11 21:43 
AnswerRe: how can i get the value of a checkbox PinmemberMostafa Kaisoun24-Aug-11 11:23 
AnswerRe: how can i get the value of a checkbox PinmemberMostafa Kaisoun29-Aug-11 12:50 
QuestionCan I import from excel file instead mdb ? Pinmemberxme8115-Mar-11 22:50 
AnswerRe: Can I import from excel file instead mdb ? PinmemberMostafa Kaisoun16-Mar-11 3:16 
GeneralError when i run it.. Pinmemberhk_maniya7-Dec-10 19:11 
GeneralRe: Error when i run it.. PinmemberMostafa Kaisoun8-Dec-10 1:02 
GeneralOLD VERSION GRID CONTROL PinmemberbilalJee31-Mar-10 20:44 
GeneralRe: OLD VERSION GRID CONTROL PinmemberMostafa Kaisoun25-Apr-10 21:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 28 Mar 2010
Article Copyright 2010 by Mostafa Kaisoun
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid