Click here to Skip to main content
12,289,457 members (73,402 online)
Click here to Skip to main content
Add your own
alternative version


105 bookmarked

Windows DataGridColumns User Controls

, 29 Nov 2004
Rate this:
Please Sign up or sign in to vote.
How to build ComboBox, DateTimePicker and Button as User Controls for DataGridColumns for WinForms.

Sample Image


This article explains how to build some special user controls for DataGrid columns on a Windows application. The special controls are: DataGrid ComboBox, DateTimePicker and Button columns. In a DataGrid, a column style is an object that defines what the column looks and behaves like, including such things as color, font, and the presence of controls that will handle linked field in a database with the use of a ComboBox, a CheckBox and other controls. The .NET Framework includes two types of column-style classes by default: the DataGridTextBoxColumn and DataGridBoolColumn classes. A quantity of jobs you may want to achieve with the Windows Forms DataGrid control are, unexpectedly, more troublesome than you might expect. Principally, this is because the Windows Forms DataGrid control is column-based, rather than cell-based. As a result, to attain most tasks, you have to work with the columns, not the cells themselves. Hence the need to create our own set of DataGridColumnStyle objects that define custom column styles for the Windows Forms DataGrid control and add them to the GridColumnStylesCollection. Below is an explanation of how those three user controls might be useful:

DataGrid ComboBox Column Style

Suppose you have a database as shown below:


You have "StudentId", a foreign key to the "Id" field in "Student" table, and another field "BookName". Now, suppose you want to display the data of "Book" on a DataGrid, and don't want to display "StudentId" as integer. In addition, you need to display the name, but any editing would update the "StudentId" field. The first solution would be to create two DataTables with parent-child relation, and write many instructions to display the wanted fields correctly, and update the table as needed. This needs a lot of instructions and design. Another solution would be to use a combo box, with data source as the "Student" table, display member as "Name", and value member as "Id". And any edits would display the new name, but update the "StudentId" field. Using the DataGridComboBoxColumn class is easy to do that, and is added as a column to the DataGrid easily.

DataGrid DateTimePicker Column Style

Let's take the same database example as above. In the table "Book", there's a field "DateTime" with date information. Suppose you want to edit this field in the DataGrid directly. One way is to write the date, in the right format, in the text box column of the DataGrid. This could cause errors if you mistyped the date, or in a different format. Another solution would be to use a DateTimePicker control, and just choose the exact date from the control. Using the DataGridTimePickerColumn allows you to achieve that easily.

DataGrid Button Column Style

You are viewing all rows in the DataGrid. Now, suppose you want to delete a row from the DataSet. A simple way would be to click on a button on this row, and an event is fired to delete the row, then refresh the DataSet. This can be easily done using DataGridButtonColumn.

Using the code


This class builds the ComboBox column user control for DataGrids for Windows applications. It inherits from DataGridTextBoxColumn, since this is already an available control in the .NET Framework. What needs to be modified, depending on the application, is the Leave event.

private void comboBox_Leave(object sender, EventArgs e)
    DataRowView rowView = (DataRowView) this.comboBox.SelectedItem;
    string s=null;
    //in case the selected value is null.
            s = (string) rowView.Row[this.comboBox.DisplayMember];
    catch(Exception ex)

    SetColumnValueAtRow(this.cmanager, this.iCurrentRow, s);

    this.DataGridTableStyle.DataGrid.Scroll -= 
        new EventHandler(DataGrid_Scroll);            

Notice that the case where the selected value is null should be handled. Null values generate System.DBNull as output. This has to be converted to an empty string.

To use this class, simply add it to the project, construct an instance, and use its properties. This is a sample usage:

RamiSaad.DataGridComboBoxColumn comboboxColStyle = 
                          new RamiSaad.DataGridComboBoxColumn(); 
comboboxColStyle.ComboBox.DisplayMember = 
comboboxColStyle.ComboBox.ValueMember = 
comboboxColStyle.HeaderText =  "Student Id";
comboboxColStyle.MappingName =  _dataSet.Tables["Book"].Columns[i].ColumnName;

Note: You must set the properties and DataSource of the ComboBox property of the user control first before setting its header text and mapping name of the column style.


This class builds the DateTimePicker column user control for DataGrids for Windows applications. It inherits from DataGridTextBoxColumn, since this is already an available control in the .NET Framework.

This is a sample usage:

RamiSaad.DataGridTimePickerColumn timepickerColStyle = 
                     new RamiSaad.DataGridTimePickerColumn();
timepickerColStyle.HeaderText =  "Date";
timepickerColStyle.MappingName =  


This class builds the Button column user control for DataGrids for Windows application. It inherits from DataGridTextBoxColumn, since this is already an available control in the .NET Framework. To use this class, simply add it to the project, construct an instance, and use its properties.

Images of the button and button-pressed are attached with the class as well. You can add images to the solution, and choose "embedded resource" as build action in their properties. Then, in the class constructor, you must specify the location of the images in the assembly's resources:

    char[] c={','};
    System.IO.Stream strm = this.GetType().Assembly.GetManifestResourceStream(
    _button = new Bitmap(strm);
    strm = this.GetType().Assembly.GetManifestResourceStream(
    _buttonPressed = new Bitmap(strm);
catch(Exception ex)

This class uses handles, thus the constructor includes column number in its parameter. You must specify the DataGrid's column number in which the button control will be inserted:

RamiSaad.DataGridButtonColumn buttonColStyle = 
  new RamiSaad.DataGridButtonColumn(i); //pass the column#

A button click event argument class is inserted in the same control's class. This is useful to manage the messages or actions when the button is clicked:

public delegate void DataGridCellButtonClickEventHandler(object sender, 
                                    DataGridCellButtonClickEventArgs e);

public class DataGridCellButtonClickEventArgs : EventArgs
    private int _row;
    private int _col;

    public DataGridCellButtonClickEventArgs(int row, int col)
        _row = row;
        _col = col;

    public int RowIndex    {get{return _row;}}
    public int ColIndex    {get{return _col;}}

Note: You must define the button click event handler DataGridCellButtonClickEventHandler. This will be called from the form itself which calls the DataGrid.

Here is a sample on how to use this class from the DataGrid's form:

    buttonColStyle = new RamiSaad.DataGridButtonColumn(i); //pass the column#
    buttonColStyle.HeaderText =  "Delete";
    buttonColStyle.MappingName =  _dataSet.Tables["Book"].Columns[i].ColumnName;

    //hookup our cellbutton handler...
    buttonColStyle.CellButtonClicked += 
    new RamiSaad.DataGridCellButtonClickEventHandler(HandleCellButtonClick);


    //hook the mouse handlers
    dataGrid1.MouseDown += new MouseEventHandler(buttonColStyle.HandleMouseDown);
    dataGrid1.MouseUp += new MouseEventHandler(buttonColStyle.HandleMouseUp);
// handler for a click on one of the gridcell buttons
private void HandleCellButtonClick(object sender, DataGridCellButtonClickEventArgs e)
    MessageBox.Show("row " + e.RowIndex.ToString() +  " will be deleted.");

Note: The handler for a button click is inserted in the DataGrid's form itself. Also, you must hook the mouse handlers to the button column style.

That's all. The user controls' classes are easy to use, as explained above. Hopefully, more user controls will be added later. Please let me know of any feedback or comment on this article or code.

Points of Interest

.NET Framework v2.0 already has these extra user controls available. When designing a DataSet, you can create many types of column styles, including those mentioned in this article. But still, sometimes it is very hard to migrate from Framework 1.1 to Framework 2.0, and thus the need to create user controls.


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


About the Author

Rami Saad
Web Developer
Ireland Ireland
Expert in Microsoft technologies.

MCP in developing windows applications. Worked in hardware/software development, system design/integration.

Currently working as Partner Technical Specialist.

You may also be interested in...

Comments and Discussions

GeneraltableStyle problem Pin
JML_4-Apr-11 22:48
memberJML_4-Apr-11 22:48 
GeneralExcelent. I Need help Pin
Member 548844731-Mar-09 12:35
memberMember 548844731-Mar-09 12:35 
QuestionProblem with ComboBox Pin
James24322228-Jan-09 3:10
memberJames24322228-Jan-09 3:10 
When you add a row with the ComboBox and try to add a new row(*) it doesn't detect that you are editing that row and doesn't want to add a new row with the values selected unless you go into a text field and start editing.
QuestionProblem with DateTimePicker Pin
James24322227-Jan-09 10:06
memberJames24322227-Jan-09 10:06 
Generalsuggested books Pin
mohitpaliwal16-Oct-08 20:59
membermohitpaliwal16-Oct-08 20:59 
Generalbutton in datagrid Pin
sharma sanjeev18-Jun-07 19:50
membersharma sanjeev18-Jun-07 19:50 
Generalrows with differen controls Pin
Giorgi Dalakishvili5-Apr-07 9:00
memberGiorgi Dalakishvili5-Apr-07 9:00 
GeneralCombo Box Pin
mykosher10-Oct-06 3:07
membermykosher10-Oct-06 3:07 
Questiondatagridcomboboxcolumn in master-detail form Pin
Naman Patel11-Aug-06 1:16
memberNaman Patel11-Aug-06 1:16 
Questionwhy datagridbutton can't use windows button contrl? Pin
beelzebub10-Jun-06 19:33
memberbeelzebub10-Jun-06 19:33 
QuestionHow to fix recursive firing of DataGrid Button Click events Pin
naruji5-Jun-06 3:02
membernaruji5-Jun-06 3:02 
Generalcompact framework Pin
csharppointer19-Apr-06 4:42
membercsharppointer19-Apr-06 4:42 
GeneralRe: compact framework Pin
jrb430@gmail.com25-Jul-07 15:09
memberjrb430@gmail.com25-Jul-07 15:09 
GeneralDataGridComboBoxColumn, DataGridComboBoxColumn Pin
L C D8-Mar-06 15:53
memberL C D8-Mar-06 15:53 
GeneralSystem.NullReferenceException Pin
hadhmy17-Jan-06 1:42
memberhadhmy17-Jan-06 1:42 
GeneralDoubt Regarding datagrid and dataview Pin
Durga Venkateswara Rao16-Jan-06 0:26
memberDurga Venkateswara Rao16-Jan-06 0:26 
QuestionWindows DataGridColumns User Controls Pin
dbn_0110-Jan-06 17:58
memberdbn_0110-Jan-06 17:58 
GeneralWindows DataGridColumns User Controls Pin
dbn_0110-Jan-06 17:56
memberdbn_0110-Jan-06 17:56 
GeneralComboBox Column of Datagrid Pin
HemaRawat27-Dec-05 22:31
memberHemaRawat27-Dec-05 22:31 
GeneralRe: ComboBox Column of Datagrid Pin
developer20097-Dec-06 19:24
memberdeveloper20097-Dec-06 19:24 
Generaldifferent button to each row Pin
kirkegor15-Nov-05 21:08
memberkirkegor15-Nov-05 21:08 
Generalerror new row with DataGridTimePickerColumn Pin
daouadji6-Oct-05 3:21
memberdaouadji6-Oct-05 3:21 
GeneralRe: error new row with DataGridTimePickerColumn Pin
Zuoliu Ding2-Nov-05 11:13
memberZuoliu Ding2-Nov-05 11:13 
GeneralRe: error new row with DataGridTimePickerColumn Pin
daouadji23-Dec-05 7:20
memberdaouadji23-Dec-05 7:20 
Questionhow to manually set a value from code Pin
ir_fuel22-Sep-05 9:51
memberir_fuel22-Sep-05 9:51 
Generalmaybe fix for mousescroll crash Pin
ir_fuel22-Sep-05 6:50
memberir_fuel22-Sep-05 6:50 
GeneralGet cell value Pin
Limit_isNo_Limit27-Aug-05 1:10
memberLimit_isNo_Limit27-Aug-05 1:10 
GeneralRequired Field In datagrid Pin
mutahar21@hotmail.com20-Aug-05 0:04
membermutahar21@hotmail.com20-Aug-05 0:04 
GeneralTab key can not stop on Datetimepicker column Pin
htphuoc197825-Jul-05 17:25
susshtphuoc197825-Jul-05 17:25 
GeneralRe: Tab key can not stop on Datetimepicker column Pin
Vlad Stanciu11-Nov-05 0:19
memberVlad Stanciu11-Nov-05 0:19 
GeneralLoosing Focus from Combobox column when we keep press tab Pin
Anji Reddy K5-Jul-05 19:13
memberAnji Reddy K5-Jul-05 19:13 
Generalrow color Pin
tvprithiv26-Jun-05 22:40
membertvprithiv26-Jun-05 22:40 
rodriguesd6-Apr-05 21:48
memberrodriguesd6-Apr-05 21:48 
GeneralFixed keyboard navigation error Pin
Mikkel Rostock27-Mar-05 4:09
memberMikkel Rostock27-Mar-05 4:09 
GeneralRe: Fixed keyboard navigation error Pin
Mark Harmon13-Sep-05 10:31
memberMark Harmon13-Sep-05 10:31 
GeneralSetColumnValueAtRow() error Pin
StephenKearney15-Mar-05 15:15
memberStephenKearney15-Mar-05 15:15 
GeneralRe: SetColumnValueAtRow() error Pin
Mark Harmon13-Sep-05 10:23
memberMark Harmon13-Sep-05 10:23 
QuestionRe: SetColumnValueAtRow() error Pin
laxhy18-Mar-06 2:24
memberlaxhy18-Mar-06 2:24 
GeneralDataGrid Relations Pin
Chiara Zachary3-Mar-05 3:11
memberChiara Zachary3-Mar-05 3:11 
GeneralRe: DataGrid Relations Pin
Rami Saad8-Mar-05 14:10
memberRami Saad8-Mar-05 14:10 
Generalnamespace Pin
Chiara Zachary23-Feb-05 6:19
memberChiara Zachary23-Feb-05 6:19 
GeneralRe: namespace Pin
Rami Saad23-Feb-05 10:13
memberRami Saad23-Feb-05 10:13 
GeneralRe: namespace Pin
Chiara Zachary23-Feb-05 22:58
memberChiara Zachary23-Feb-05 22:58 
GeneralError on adding new row Pin
lagor23-Feb-05 3:21
memberlagor23-Feb-05 3:21 
GeneralRe: Error on adding new row Pin
Rami Saad28-Feb-05 11:32
memberRami Saad28-Feb-05 11:32 
GeneralError when not selecting a value from the Combobox Pin
lud1233-Feb-05 20:56
memberlud1233-Feb-05 20:56 
GeneralRe: Error when not selecting a value from the Combobox Pin
Rami Saad4-Feb-05 3:33
memberRami Saad4-Feb-05 3:33 
GeneralRe: Error when not selecting a value from the Combobox Pin
lud1234-Feb-05 20:28
memberlud1234-Feb-05 20:28 
GeneralRe: Error when not selecting a value from the Combobox Pin
Rami Saad5-Feb-05 0:33
memberRami Saad5-Feb-05 0:33 
GeneralRe: Error when not selecting a value from the Combobox Pin
lud1235-Feb-05 21:57
memberlud1235-Feb-05 21:57 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160518.1 | Last Updated 30 Nov 2004
Article Copyright 2004 by Rami Saad
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid