Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox in datagrid 2

Status
Not open for further replies.

groston

IS-IT--Management
Dec 31, 2001
141
US
For a Windows application (not ASP), I wish to present a two column datagrid. The first column shows student's names and the seocnd should have a listbox that lists the professors' names. My plan is to have the user select a single professor for each student. Upon clicking a fished button, each row of the of the data grid (one student name and one professor name) will be written to a database table.

Sound easy, but...

I haven't go a clue as to how to embed the listbox within the datagrid. Could you please provide me a pointer to a solution. (What is so irksome is that this is relatively easy using ASP.NET...)

Thanks for your help.

----

Gerry Roston
gerry@pairofdocs.net
 
This example uses a combobox instead of a listbox. Not my code, I found this in a newgroup a while ago. Assuming you have a connection and data adapter up and running, this works real nice. Good Luck

Public MyCombo As New ComboBox
Public ds As New DataSet()

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler MyCombo.TextChanged, AddressOf Ctrls_TextChanged
'Fill ComboBox list.
MyCombo.Name = "MyCombo"
MyCombo.Visible = False
MyCombo.Items.Clear()
MyCombo.Items.Add("Monday")
MyCombo.Items.Add("Tuesday")
MyCombo.Items.Add("Wednesday")
MyCombo.Items.Add("Thursday")
MyCombo.Items.Add("Friday")


daEmp.Fill(ds, "MyTable")

'Set the RowHeight of the DataGrid to the height of the ComboBox.
DataGrid1.PreferredRowHeight = MyCombo.Height

DataGrid1.DataSource = ds

DataGrid1.DataMember = "MyTable"
'Add ComboBox to the Control collection of the DataGrid.
DataGrid1.Controls.Add(MyCombo)
End Sub

Private Sub DataGrid1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGrid1.Paint
If DataGrid1.CurrentCell.ColumnNumber = 1 Then
MyCombo.Width = DataGrid1.GetCurrentCellBounds.Width
End If
End Sub

Private Sub Ctrls_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
If DataGrid1.CurrentCell.ColumnNumber = 1 Then
MyCombo.Visible = False
If DataGrid1.Item(DataGrid1.CurrentCell) & "" = "" Then
SendKeys.Send("*")
End If
DataGrid1.Item(DataGrid1.CurrentCell) = MyCombo.Text
End If
End Sub

Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
If DataGrid1.CurrentCell.ColumnNumber = 1 Then
MyCombo.Width = 0
MyCombo.Left = DataGrid1.GetCurrentCellBounds.Left
MyCombo.Top = DataGrid1.GetCurrentCellBounds.Top
MyCombo.Text = DataGrid1.Item(DataGrid1.CurrentCell) & ""
MyCombo.Visible = True
Else
MyCombo.Visible = False
MyCombo.Width = 0
End If
End Sub

Private Sub DataGrid1_Scroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Scroll
MyCombo.Visible = False
MyCombo.Width = 0
End Sub

Private Sub DataGrid1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Click
MyCombo.Visible = False
MyCombo.Width = 0
End Sub
 
cmn2

Thanks for the help!

It took some time, but I got it working.

This code could be improved by figuring out dynamically how to determine the column to use (instead of hard-coding in the 1). It also, initially, places the combobox at the upper-left of the datagrdi - weird. Finally, instead of using Ctrls_TextChanged, I created a handler for SelectedIndexChanged for the combobox.


----

Gerry Roston
gerry@pairofdocs.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top