Click here to Skip to main content
15,886,799 members
Articles / Programming Languages / Visual Basic
Article

How to add selection controls to your DataGrid

Rate me:
Please Sign up or sign in to vote.
3.39/5 (12 votes)
10 Feb 20052 min read 93.6K   586   31   12
This article describes how to add controls to a datagrid. This will add controls at runtime.

Sample Image - Demo.gif

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 some some columns of data grid from a database/Xml file to the datagrid and add these 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 what ever you want when firing those events.

Background

When I was asked to develop a configuration screen for a application I have faced so many difficulties while searching for 'how to add controls to the data grid'. I couldn't find any good complete article on this. After doing 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 is loading through a MSSQL Server ® database you need to change those server settings accordingly. (Server Name, User Id and password).

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

VB
//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.

VB
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

Capturing events and getting values to the DataGrid on those controls. (This code fragment shows you how to get value from a DateTimePicker and place it on the selected cell.)

VB
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.

VB
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.

Conclusion

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 - 11/Feb/2005

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
Team Leader
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionPrivate WithEvents FLX As EditableGrid Pin
Member 1270965412-Sep-16 5:51
Member 1270965412-Sep-16 5:51 
Questionhelp me... Pin
vijislak17-Jun-12 0:02
vijislak17-Jun-12 0:02 
GeneralProblem in Columns model Pin
Vitoto16-Jan-06 13:10
Vitoto16-Jan-06 13:10 
GeneralThanks very much Pin
salamonty13-Dec-05 1:20
salamonty13-Dec-05 1:20 
Questionhow to close a form from another ? Pin
mushroom15vn21-Jun-05 0:11
mushroom15vn21-Jun-05 0:11 
QuestionHow to determine which menuItem is clicked? Pin
Anonymous19-Jun-05 17:12
Anonymous19-Jun-05 17:12 
GeneralGreat Work Pin
Prakash_vins4-Apr-05 22:28
Prakash_vins4-Apr-05 22:28 
GeneralI get error Pin
vsing24-Mar-05 0:51
vsing24-Mar-05 0:51 
GeneralNew record question using your code Pin
Tony Zaradich7-Mar-05 9:19
Tony Zaradich7-Mar-05 9:19 
GeneralRe: New record question using your code Pin
andrewcsk3-May-05 8:02
andrewcsk3-May-05 8:02 
Generalyou will have a memory problem Pin
Tom Janssens10-Feb-05 22:18
Tom Janssens10-Feb-05 22:18 
If you add a new control everytime you edit a cell, you will get memory problems.
You need to dispose the controls after use, and set them to null every time, or even better, reuse the same controls.
It is also common practice to use as little controls as possible, so when you have f.e. a combo box in two cells, use the same combo control for both cells, but simply change the databinding & postioning.


Tom

Core
GeneralRe: you will have a memory problem Pin
Buddhi Dananjaya13-Feb-05 17:19
Buddhi Dananjaya13-Feb-05 17:19 

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.