ARTICLE

Adding controls to a DataGrid at runtime

Posted by Tushar Ameta Articles | Visual Basic 2010 June 24, 2003
This article covers some topics where user can add any control based on his/her choice and can use it to edit the details in a DataGrid.
 
Reader Level:

While developing application software, representing data in the GUI in friendly and readable format is one of the most important considerations.

In VB.NET application (or Windows/desktop applications), .NET provides Data grid control to achieve the above important and vital consideration. Sometimes while representing data in a data grid control, user wants to edit the details i.e. he/she wants to edit the details through a combo control or a date time picker control etc.

This article below sheds some light in above area i.e. user can add any control based on his/her choice and can use it to edit the details in the data grid.

Load the Grid with details (when the button Load Grid is clicked).

'Establish the connection to the data base and open it
Dim sqlConn As New SqlConnection("Database=****; Server=*****; uid = **; pwd=****") '*-Pass the required details
sqlConn.Open()
'create the sql command object and set its command type to //execute the sql query to get the //results
Dim sc As New SqlCommand()
sc.Connection = sqlConn
sc.CommandType = CommandType.Text
sc.CommandText = "SELECT ControlName,Control,Description FROM t_TestControls"
'create the data set object to be used to fill the data grid //with the data
Dim ds As New DataSet()
'Create the sql adapter that will be used to fill the data //set created above
Dim myReader As New SqlDataAdapter(sc)
myReader.Fill(ds)
'Fill the rows in the grid
Dim i As Integer
For
i = 0 To (ds.Tables(0).Rows.Count) - 1
dataTable.LoadDataRow(arrstr,
True)
datagrid1(i, 0) = ds.Tables(0).Rows(i).ItemArray(0).ToString()
datagrid1(i, 1) = ds.Tables(0).Rows(i).ItemArray(1).ToString()
datagrid1(i, 2) = ds.Tables(0).Rows(i).ItemArray(2).ToString()
Next i



Add a control to the data grid.

'ADD the below code in the Got focus event of the data grid text box column
' To add any control ,create its object,set its property and add it to the respective column
comboControl = New ComboBox()
comboControl.Cursor = System.Windows.Forms.Cursors.Arrow
comboControl.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown
comboControl.Dock = DockStyle.Fill
comboControl.Items.AddRange(
New String(5) {"", "Information Technology", "Computer Science", "Bio Technology", "Electrical Engg"})
'Create the date time picker control to be added and set its properties
Dim dtp As New DateTimePicker()
dtp.Dock = DockStyle.Fill
dtp.Cursor = Cursors.Arrow
'Create the check box control to be added and set its properties
Dim chk As New CheckBox()
chk.Dock = DockStyle.Fill
chk.Cursor = Cursors.Arrow
'Create the radio button control to be added and set its properties
Dim rb As New RadioButton()
rb.Dock = DockStyle.Fill
rb.Cursor = Cursors.Arrow
'Add the controls to the respective columns in the data grid
Dim i As Integer
For
i = 0 To dataTable.Rows.Count - 1
'if the data in the first column is date time, add a date time control to the grid
If datagrid1(i, 0).ToString().Equals("DateTime") And Not (hitTestGrid Is Nothing) And hitTestGrid.Row = i Then
datagridtextBox.TextBox.Controls.Add(dtp)
comboControl.SendToBack()
chk.SendToBack()
rb.SendToBack()
dtp.BringToFront()
'if the data in the first column is combo box, add a combo box control to the grid
Else
If
datagrid1(i, 0).ToString().Equals("ComboBox") And Not (hitTestGrid Is Nothing) And hitTestGrid.Row = i Then
datagridtextBox.TextBox.Controls.Add(comboControl)
chk.SendToBack()
dtp.SendToBack()
rb.SendToBack()
comboControl.BringToFront()
End If
End
If
datagridtextBox.TextBox.BackColor = Color.White
Next i

NOTE : hitTestGrid can be obtained
in the mouse down event
of the data grid.



For complete source code, please see the attached file.

Login to add your contents and source code to this article
share this article :
post comment
 

How about user control insert into datagridview ?? may i have some example ?

Posted by Firman Putra Jun 16, 2010

Good .. ..

Posted by Firman Putra Jun 16, 2010

how to add checkbox in datagrid windows mobile device

Posted by veera Feb 08, 2010

I hv add a system.windows.forms namespace but it does not display datagridtextBox property, I want to add button controll in datagrid for pocket pc(PDA Application) please reply me soon Thanks in advance

Posted by sonia gupta May 10, 2007
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor