Click here to Skip to main content
15,886,258 members
Articles / Programming Languages / C#
Article

Enhanced DataGrid control

Rate me:
Please Sign up or sign in to vote.
4.20/5 (19 votes)
24 Jun 20032 min read 188.3K   7.9K   66   17
Extends DataGrid Functionalities

Sample Image - GridEx.jpg

Introduction

This is an enhanced DataGrid that built in with Auto Search, Auto Column Sizing and Row Select features. In addition, it supports multiple Tablestyles binding to single DataTable. New events such as CurrentRowChanged and OnHeaderClick also have been included.

Background

This component was created out of frustration with default datagrid controls. After searching around from web but could not find a Free datagrid control with the features that suit my needs. I decided to create one by myself. This is my first .NET component, feel free to give me feedbacks or comments so that I can learn something and improve my .NET skill. I hope this component is useful to those of you who are as frustrated as me.

GridEx

PropertyDescription
AutoSearchEnable or disable auto search function in datagrid.
AutoSizeEnable or disable column auto sizing in datagrid.
RowSelectEnable or disable row select in datagrid.
CurrentStyeIndexGets or sets a value indicating current TableStyle. Default value is -1.
UseGridParentStyleGets or sets a value indicating whether TableStyles should follow the grid style.

MethodDescription
OnHeaderClickRaises HeaderClicked event.
OnCurrentRowChangedRaises CurrentRowChanged event.

EventDescription
CurrentRowChangedOccurs when current row has changed.
HeaderClickedOccurs when grid header is clicked.

How to use AutoSearch in GridEx

Set AutoSearch property to true during design time or use the following code during runtime:-

C#
...
this.gridEx1.AutoSearch = true;

During runtime, simple click on the header column that you want to perform search and then type in search value and GridEx will perform incremental search for you. You may use [Backspace] to delete char or [Escape] to clear search value. If you search on DateTime column type, a datetime input box will appear.

Image 2

Creating Multiple TableStyles

Create styles using datagrid TableStyles Editor. Set the style's MappingName to TableName of DataTable if you want GridEX to load this style first. If none of the styles has MappingName assigned, default style will be used.

Image 3

Changing TableStyles at Runtime

To change style at runtime, set CurrentStyleIndex to a valid TableStye index.

C#
...
this.gridEx1.CurrentStyleIndex = 1;

Setting CurrentStyleIndex to -1 will tell GridEx to use default style.

Limitation

AutoSearch only supports DataTable and DataView datasource. AutoSearch will be turned off automatically if you assign a DataSet to datagrid DataSource property.

Contact

I can be reached at cheetuan_choong@jabil.com

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Malaysia Malaysia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCalender Cells Pin
sashdude1-Oct-08 7:39
sashdude1-Oct-08 7:39 
GeneralColumnName with a space Pin
KC5HHQ12-Jan-07 3:52
KC5HHQ12-Jan-07 3:52 
GeneralDisable a column in datagrid Pin
Ndichuw Samuel21-Jun-06 1:05
Ndichuw Samuel21-Jun-06 1:05 
GeneralRe: Disable a column in datagrid Pin
KC5HHQ12-Jan-07 3:51
KC5HHQ12-Jan-07 3:51 
GeneralRe: Disable a column in datagrid Pin
altaf sami4-Apr-08 19:15
altaf sami4-Apr-08 19:15 
QuestionHow i retrive data from Cell [modified] Pin
sachinjr14-Jun-06 0:40
sachinjr14-Jun-06 0:40 
AnswerRe: How i retrive data from Cell Pin
danish_bear20-Jun-06 9:32
danish_bear20-Jun-06 9:32 
GeneralRe: How i retrive data from Cell Pin
KC5HHQ12-Jan-07 3:54
KC5HHQ12-Jan-07 3:54 
AnswerRe: How i retrive data from Cell Pin
altaf sami4-Apr-08 19:19
altaf sami4-Apr-08 19:19 
GeneralAutosize only works with DataSets, DataTables, and DataViews but not with DataRelations Pin
Josef Meile8-May-06 9:37
Josef Meile8-May-06 9:37 
<lots of="" unuseful="" code="" were="" removed="">

I decided to change this post 'cause I found that what I was doing before was innecessary. In fact there is an easiest way to achieve it, so, I update it to reflect what I finally did.

-- modified at 16:48 Monday 8th May, 2006

Hi,

First at all, thanks for this nice control. I only have a but:

This functionality assumes that the user is only going to use DataSets, DataTables, and DataViews as DataSources.

In fact, you can also use DataRelations and other types. In my case, I'm using a DataGrid set as follows:

grid.DataSource = dbDataSet;
grid.SetDataBinding(dbDataSet,"ActorInstance.ActorInstOutputInst");

The main table is named ActorInstance and its related records are in another table called "OutputInstance". I created a DataRelation called "ActorInstOutputInst", which relates these two tables. Using a normal DataGrid shows me the parent record and its related rows
in the DataGrid. As soon as I change the DataGrid for your class, I get a NullReferenceException in the following line:

dv = ((DataSet)this.DataSource).Tables[this.DataMember].DefaultView;

The problem here is that this.DataMember is equal to "ActorInstance.ActorInstOutputInst", which is a DataRelation and not a DataTable, so, it isn't in the "Tables" array from DataSource.

For the interested, I changed the code in order to make it work with DataRelations as well. I don't know how efficient it is, so, if you have a better way of doing this, please let me now (Perhaps there is a better way of doing this with .net 2.0, but at the moment I'm forced
to use .net 1.1).

I replaced the CurrentView Property definition of the CGridEx class by:

protected DataView CurrentView
{
get
{
DataView cv = null;
if ((this.DataSource is DataSet) && (this.DataMember.Length > 0))
{
// Turn off AutoSearch if DataSource is DataSet
// Why did you do this? I also tested AutoSearch = true with
// my setup and it worked.
// this.AutoSearch = false;

int dotPos;
if ((dotPos = this.DataMember.LastIndexOf(".")) >= 0)
{
//There is at least a dot in the DataMember string, which means
//that the DataGrid is bound to a DataRelation

//I found this hack to get the DataView of a DataRelation. It seems to work
CurrencyManager cm = (CurrencyManager)BindingContext[this.DataSource,this.DataMember];
cv = ((DataView)cm.List);
}
else
cv = ((DataSet)this.DataSource).Tables[this.DataMember].DefaultView;
}
else if (this.DataSource is DataTable)
cv = ((DataTable)this.DataSource).DefaultView;
else if (this.DataSource is DataView)
cv = (DataView)this.DataSource;

return cv;
}
}

Then It worked.

I have a final question:
1) Why do you set AutoSearch to false when the DataGrid is bound to a DataSet? Did you find a bug? I tried with AutoSerch = true and it seemed to work.

Best Regards
Josef
GeneralAutosearch Pin
u999nik6-Nov-03 11:15
u999nik6-Nov-03 11:15 
GeneralQuerying the database Pin
sayyedabdulrahim6-Sep-03 5:35
sayyedabdulrahim6-Sep-03 5:35 
GeneralWhy &quot;ReadOnly&quot; Pin
scampbell15-Sep-03 17:17
scampbell15-Sep-03 17:17 
GeneralProblem with AutoSize Pin
ikos18-Jul-03 5:47
ikos18-Jul-03 5:47 
GeneralAnnouncement !!! Pin
cctuan25-Jun-03 15:12
cctuan25-Jun-03 15:12 
GeneralError running demo project Pin
Drew Stainton25-Jun-03 7:43
Drew Stainton25-Jun-03 7:43 
GeneralRe: Error running demo project Pin
Michael Choong25-Jun-03 14:32
Michael Choong25-Jun-03 14:32 

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

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