Placing ComboxBox in DataGrid Cell -Visual Basic .Net Windows Forms

Shalvin 0 Tallied Votes 141 Views Share

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
debashishm 0 Newbie Poster

i am using vb.net for the first time...can any one of u help me in how to add values from the database and display it into a textbox.
the columns afr bill_no((varchar)not a primary key), customer_no and prod_amount(money).

please help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.