Click here to Skip to main content
11,932,168 members (62,000 online)
Click here to Skip to main content
Add your own
alternative version


75 bookmarked

How to add selection controls to your DataGrid

, 11 Feb 2005
Rate this:
Please Sign up or sign in to vote.
This article describes how to add controls to a DataGrid. This will add controls at runtime. I hope this will be helpful for developers who are using DataGrids for data entry forms.



This will help you to understand how to add any type of control (ComboBox, DateTimePicker, CheckBox, RadioButton, etc.) to your DataGrid control. If you want, you can load data into some columns of the DataGrid from a database/XML file and add the controls to a separate column as a selection. By doing this, it will help you to reduce coding needed for validation since you are limiting data entry to a selection.

In the same time, you can capture events of those added controls and do whatever you want when firing those events.


When I was asked to develop a configuration screen for an application, I faced so many difficulties while searching for 'how to add controls to the DataGrid'. I couldn't find any good complete article on this. After doing a long research on this, I was able to make a good methodology to do that. Without keeping it as a secret, I would like to share it with you all.

Using the code

The attached application contains all the code needed to run. Since some of the columns of the DataGrid are loading through a MS SQL Server ® database, you need to change the server settings accordingly (server name, user ID and password).

And run the attached script on the Query Analyzer to create the database on the SQL Server.

//Capturing clicked cell into a locally defined variable.
Private hitTestGrid As DataGrid.HitTestInfo

All the controls which you wish to have on the DataGrid should be declared along "WithEvents" keyword, only if you are interested with their events to be captured.

Private WithEvents datagridtextBox As DataGridTextBoxColumn
Private WithEvents dataTable As dataTable
Private WithEvents comboControl As System.Windows.Forms.ComboBox
Private WithEvents dtp As New DateTimePicker
Private WithEvents chk As New CheckBox

Now we will see about capturing events and getting values to the DataGrid. (This code fragment shows you how to get value form a DateTimePicker and place it on the selected cell.)

Private Sub dtp_ValueChanged(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles dtp.ValueChanged
    dgMember(hitTestGrid.Row, hitTestGrid.Column) = dtp.Value.ToString
End Sub

Looping through available columns and determining on which cell you have clicked:

For i = 0 To dataTable.Rows.Count - 1
    sType = dgMember(i, 0).ToString()
    If hitTestGrid.Row = i Then
        Select Case hitTestGrid.Row
            Case 1
            Case 0
            Case 2
            Case 3
        End Select
    End If
    datagridtextBox.TextBox.BackColor = Color.White
Next i

Please look into the code, by that you will get a better picture about the concept.

Points of Interest

I learnt a lot about new features on .NET while writing this code. And I hope to do my VB.NET certification next week.


Initial version.


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

Buddhi Dananjaya
Team Leader
Sri Lanka Sri Lanka
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 3 Pin
tigercont23-Jan-12 23:45
membertigercont23-Jan-12 23:45 
QuestionSelection control in Datagrid Pin
infotools13-Nov-07 18:45
memberinfotools13-Nov-07 18:45 
AnswerRe: Selection control in Datagrid Pin
infotools16-Nov-07 0:15
memberinfotools16-Nov-07 0:15 
QuestionDisk full error in vb 6.0 and VB.NET 2003 Pin
ketanani31-Aug-07 2:56
memberketanani31-Aug-07 2:56 
Questionhow to handle key left/right/up/down? Pin
sonixs31-Jul-07 4:44
membersonixs31-Jul-07 4:44 
GeneralVery good job Pin
alfigueroa25-Jun-07 8:02
memberalfigueroa25-Jun-07 8:02 
GeneralRe: Very good job Pin
Buddhi Dananjaya25-Jun-07 18:12
memberBuddhi Dananjaya25-Jun-07 18:12 
QuestionHow to write Keyperss event for UP/ DOWN Keys in DatagridtextColumn Pin
ketanani27-Apr-07 4:09
memberketanani27-Apr-07 4:09 
Generalur work is great Pin
ManeeshOhm3-Jan-07 1:36
memberManeeshOhm3-Jan-07 1:36 
Generalgreat work Pin
ManeeshOhm3-Jan-07 1:34
memberManeeshOhm3-Jan-07 1:34 
Generalapp fails on reorder of the columns Pin
ra00l2-Sep-06 6:27
memberra00l2-Sep-06 6:27 
Generalcan't see my picbox control unless I click in a cell Pin
Les Stockton10-Aug-06 5:26
memberLes Stockton10-Aug-06 5:26 
Questionhow to add combobox in datagrid.i use following.but i can't work.any idea about this send it me Pin
vidyashankar18-Jul-06 0:31
membervidyashankar18-Jul-06 0:31 
AnswerRe: how to add combobox in datagrid.i use following.but i can't work.any idea about this send it me Pin
infotools16-Nov-07 0:17
memberinfotools16-Nov-07 0:17 
QuestionHow to handle typed text combo. Pin
RDoes18-Mar-06 6:42
memberRDoes18-Mar-06 6:42 
GeneralDateTimePicker & Access Pin
Vitoto16-Jan-06 13:19
memberVitoto16-Jan-06 13:19 
GeneralCopied version of Your Article Pin
HemaRawat22-Dec-05 22:50
memberHemaRawat22-Dec-05 22:50 
GeneralNeed More Documentation Pin
HemaRawat22-Dec-05 22:42
memberHemaRawat22-Dec-05 22:42 
QuestionHow to use this control to in usercontrol Pin
HarSha9919-Oct-05 4:41
memberHarSha9919-Oct-05 4:41 
QuestionHow to add selection countrols to your datagrid? But it has the problem? Pin
myphuong2441-Sep-05 0:49
membermyphuong2441-Sep-05 0:49 
AnswerRe: How to add selection countrols to your datagrid? But it has the problem? Pin
master_pine11-Jun-06 19:35
membermaster_pine11-Jun-06 19:35 
QuestionCombo padding? Pin
Aranda L Morrison21-Jun-05 19:25
memberAranda L Morrison21-Jun-05 19:25 
AnswerRe: Combo padding? Pin
shuxuzhen3-Feb-06 21:39
membershuxuzhen3-Feb-06 21:39 

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
Web04 | 2.8.151126.1 | Last Updated 11 Feb 2005
Article Copyright 2005 by Buddhi Dananjaya
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid