Ver Mensaje Individual
  #20 (permalink)  
Antiguo 01/08/2012, 10:33
leochavez
 
Fecha de Ingreso: febrero-2012
Ubicación: San juan de luriguashington
Mensajes: 39
Antigüedad: 12 años, 9 meses
Puntos: 2
Respuesta: filtrar combox dentro de datagridview vb net

SOLUCIONADO GRACIAS

Imports System.Data.SqlClient
Public Class Form1
Dim dv As DataView
Dim ds As New DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
Dim strConn As String
Dim da, daProducts As SqlDataAdapter
Dim conn As SqlConnection
strConn = "Data Source=172.0.27.8;Initial Catalog=Northwind;Integrated Security=True"
conn = New SqlConnection(strConn)
da = New SqlDataAdapter("Select * from Categories", conn)
daProducts = New SqlDataAdapter("Select * from Products", conn)
da.Fill(ds, "Categories")
daProducts.Fill(ds, "Products")
dt.Columns.Add("Category", GetType(Integer))
dt.Columns.Add("Product")
DataGridView1.AutoGenerateColumns = False
DataGridView1.DataSource = dt
Dim dgvCombo As New DataGridViewComboBoxColumn
With dgvCombo
.Width = 150
.DataSource = ds.Tables("Categories")
.DisplayMember = "CategoryName"
.DataPropertyName = "CategoryID"
.ValueMember = "CategoryID"
.HeaderText = "Category"
End With
dv = New DataView(ds.Tables("Products"))
DataGridView1.Columns.Add(dgvCombo)
Dim dgvFilter As New DataGridViewComboBoxColumn
With dgvFilter
.Width = 150
.DisplayMember = "ProductName"
.DataPropertyName = "ProductName"
.HeaderText = "Product"
End With
DataGridView1.Columns.Add(dgvFilter)
End Sub

Private Sub DataGridView1_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventAr gs) Handles DataGridView1.CellBeginEdit
If e.ColumnIndex = 1 Then
Dim dgvCbo As DataGridViewComboBoxCell = TryCast(DataGridView1(e.ColumnIndex, e.RowIndex), DataGridViewComboBoxCell)
If dgvCbo IsNot Nothing Then
Dim str As String = DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value.ToString
dv = New DataView(ds.Tables("Products"))
dv.RowFilter = "CategoryID = " & str
dgvCbo.DataSource = dv
End If
End If
End Sub

Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
If e.ColumnIndex = 2 Then
Dim dgvCbo As DataGridViewComboBoxCell = TryCast(DataGridView1(e.ColumnIndex, e.RowIndex), DataGridViewComboBoxCell)
If dgvCbo IsNot Nothing Then
dv = New DataView(ds.Tables("Products"))
dgvCbo.DataSource = dv
End If
End If
End Sub

Private Sub DataGridView1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArg s) Handles DataGridView1.DataError
e.Cancel = True
End Sub

Private Sub DataGridView1_DefaultValuesNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles DataGridView1.DefaultValuesNeeded
e.Row.Cells(0).Value = 1
End Sub
End Class