Friday, April 8, 2011

How to add selection controls to your DataGrid – Dot net 1.1

 

Source:

http://www.codeproject.com/KB/vb/SelectionCtlsDataGrid.aspx 

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

http://www.codeproject.com/KB/vb/SelectionCtlsDataGrid/DataGrid.zip

No comments: