Click here to Skip to main content
Licence 
First Posted 11 Feb 2005
Views 135,688
Bookmarked 74 times

How to add selection controls to your DataGrid

By | 11 Feb 2005 | Article
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.
 
Part of The SQL Zone sponsored by
See Also

Sample

Introduction

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.

Background

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
                datagridtextBox.TextBox.Controls.Add(dtp)
                dtp.BringToFront()
            Case 0
                datagridtextBox.TextBox.Controls.Add(comboControl)
                comboControl.BringToFront()
            Case 2
                datagridtextBox.TextBox.Controls.Add(chk)
                chk.BringToFront()
            Case 3
                datagridtextBox.TextBox.Controls.Add(rb)
                rb.BringToFront()
        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.

History

Initial version.

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

About the Author

Buddhi Dananjaya

Team Leader

Sri Lanka Sri Lanka

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 3 Pinmembertigercont22:45 23 Jan '12  
QuestionSelection control in Datagrid Pinmemberinfotools17:45 13 Nov '07  
AnswerRe: Selection control in Datagrid Pinmemberinfotools23:15 15 Nov '07  
QuestionDisk full error in vb 6.0 and VB.NET 2003 Pinmemberketanani1:56 31 Aug '07  
Questionhow to handle key left/right/up/down? Pinmembersonixs3:44 31 Jul '07  
GeneralVery good job Pinmemberalfigueroa7:02 25 Jun '07  
GeneralRe: Very good job PinmemberBuddhi Dananjaya17:12 25 Jun '07  
QuestionHow to write Keyperss event for UP/ DOWN Keys in DatagridtextColumn Pinmemberketanani3:09 27 Apr '07  
Generalur work is great PinmemberManeeshOhm0:36 3 Jan '07  
Generalgreat work PinmemberManeeshOhm0:34 3 Jan '07  
Generalapp fails on reorder of the columns Pinmemberra00l5:27 2 Sep '06  
Generalcan't see my picbox control unless I click in a cell PinmemberLes Stockton4:26 10 Aug '06  
Questionhow to add combobox in datagrid.i use following.but i can't work.any idea about this send it me Pinmembervidyashankar23:31 17 Jul '06  
AnswerRe: how to add combobox in datagrid.i use following.but i can't work.any idea about this send it me Pinmemberinfotools23:17 15 Nov '07  
QuestionHow to handle typed text combo. PinmemberRDoes5:42 18 Mar '06  
GeneralDateTimePicker & Access PinmemberVitoto12:19 16 Jan '06  
GeneralCopied version of Your Article PinmemberHemaRawat21:50 22 Dec '05  
GeneralNeed More Documentation PinmemberHemaRawat21:42 22 Dec '05  
QuestionHow to use this control to in usercontrol PinmemberHarSha993:41 19 Oct '05  
QuestionHow to add selection countrols to your datagrid? But it has the problem? Pinmembermyphuong24423:49 31 Aug '05  
AnswerRe: How to add selection countrols to your datagrid? But it has the problem? Pinmembermaster_pine18:35 11 Jun '06  
QuestionCombo padding? PinmemberAranda L Morrison18:25 21 Jun '05  
AnswerRe: Combo padding? Pinmembershuxuzhen20:39 3 Feb '06  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 11 Feb 2005
Article Copyright 2005 by Buddhi Dananjaya
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid