DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   VB.NET (http://www.daniweb.com/forums/forum58.html)
-   -   Code Snippet: Placing ComboxBox in DataGrid Cell -Visual Basic .Net Windows Forms (http://www.daniweb.com/forums/thread217042.html)

Shalvin Feb 21st, 2008 3:01 am
Placing ComboxBox in DataGrid Cell -Visual Basic .Net Windows Forms
 
Placing ComboxBox in DataGrid Cell is a task usually found in Invoicing application. Here is my version of same.

  1. Imports System.Data.SqlClient
  2. Public Class Form1
  3. Dim cn As SqlConnection
  4. Dim cmd As SqlCommand
  5. Dim da As SqlDataAdapter
  6. Dim dr As SqlDataReader
  7. Dim ds As New DataSet
  8.  
  9.  
  10. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  11. cn = New SqlConnection("Integrated Security=sspi;Initial Catalog =ShalvinPD;Data Source = .\sqlexpress")
  12. cn.Open()
  13. da = New SqlDataAdapter("select ProductName,Categoryid ,UnitPrice from Products", cn)
  14. da.Fill(ds, "products")
  15. DataGrid1.SetDataBinding(ds, "products")
  16. fillcombo()
  17. End Sub
  18.  
  19.  
  20. Private Sub DataGrid1_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
  21. Dim x, y, w, h As Integer
  22. Dim rect As Rectangle
  23. rect = DataGrid1.GetCurrentCellBounds
  24. x = rect.X + DataGrid1.Left
  25. y = rect.Y + DataGrid1.Top
  26. w = rect.Width
  27. h = rect.Height
  28. ComboBox1.SetBounds(x, y, w, h)
  29. ComboBox1.Visible = True
  30. End Sub
  31. Private Sub fillcombo()
  32. cmd = New SqlCommand("select * from categories", cn)
  33. dr = cmd.ExecuteReader()
  34. While dr.Read()
  35. ComboBox1.Items.Add(dr("categoryName"))
  36. End While
  37. End Sub
  38.  
  39. Private Sub DataGrid1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseDown
  40. MessageBox.Show(e.X.ToString() + " " + e.Y.ToString() + " " + e.Button.ToString())
  41. End Sub
  42. End Class
  43.  
  44.  
  45. Happy Programming

All times are GMT -4. The time now is 4:25 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC