| Shalvin | visualbasicnet syntax Feb 21st, 2008 | |
| Placing ComboxBox in DataGrid Cell is a task usually found in Invoicing application. Here is my version of same. |
Imports System.Data.SqlClient
Public Class Form1
Dim cn As SqlConnection
Dim cmd As SqlCommand
Dim da As SqlDataAdapter
Dim dr As SqlDataReader
Dim ds As New DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cn = New SqlConnection("Integrated Security=sspi;Initial Catalog =ShalvinPD;Data Source = .\sqlexpress")
cn.Open()
da = New SqlDataAdapter("select ProductName,Categoryid ,UnitPrice from Products", cn)
da.Fill(ds, "products")
DataGrid1.SetDataBinding(ds, "products")
fillcombo()
End Sub
Private Sub DataGrid1_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
Dim x, y, w, h As Integer
Dim rect As Rectangle
rect = DataGrid1.GetCurrentCellBounds
x = rect.X + DataGrid1.Left
y = rect.Y + DataGrid1.Top
w = rect.Width
h = rect.Height
ComboBox1.SetBounds(x, y, w, h)
ComboBox1.Visible = True
End Sub
Private Sub fillcombo()
cmd = New SqlCommand("select * from categories", cn)
dr = cmd.ExecuteReader()
While dr.Read()
ComboBox1.Items.Add(dr("categoryName"))
End While
End Sub
Private Sub DataGrid1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseDown
MessageBox.Show(e.X.ToString() + " " + e.Y.ToString() + " " + e.Button.ToString())
End Sub
End Class
Happy Programming