
Eliminar datos en ComboBox .net con datasource
Publicado por Emilio (12 intervenciones) el 14/04/2014 15:40:28
Tengo 2 combobox enlazados y lo que pasa que no me hace la eliminacion de los datos cuando cambio el primero, los datos se van agregando debajo de los que estaban antes. he probado todas las soluciones que he visto en internet y no encuentro ninguna solucion.
las ya probadas:
cboCombo.DataSource = Nothing
cboCombo.ValueMember = Nothing
cboCombo.DisplayMember = Nothing
cboCombo.DropDownWidth = 1
cboCombo.SelectedValue = Nothing
cboCombo.Text = Nothing
cboCombo.items.clear()
Dim c, x As Double
c = cboCombo.Items.Count
While x < c
x = x + 1
cboCombo.Items.RemoveAt(x - 1)
End While
y ninguna funciona espero que me ayuden
codigo de la BD
' agregar primer combobox
Public Function obteneridholding() As DataSet
conn.Conectar()
Dim sql As String = "SELECT idholding,nombre FROM Holding"
cmd = New SqlCommand(sql, conn.conector)
dt.SelectCommand = cmd
dt.Fill(ds, "Holding")
conn.Desconectar()
Return ds
End Function
' agregar al combobox dependiente
Public Function obteneridsociedadporholding(ByVal id As Integer) As DataSet
holdin.id = id
obteneridholdingPorID(holdin)
Dim id2 As Integer
id2 = Convert.ToInt32(id)
Dim sql As String = "SELECT idsociedad,nombre FROM Sociedad WHERE idholding = '" & holdin.codigo & "'"
conn.Conectar()
cmd = New SqlCommand(sql, conn.conector)
dt.SelectCommand = cmd
dt.Fill(ds, "Sociedad")
Return ds
conn.Desconectar()
End Function
' codigo del formulario
Private Sub IngresoInfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cbholding.DataSource = gestionsol.obteneridholding().Tables("Holding")
cbholding.DisplayMember = "nombre"
cbholding.ValueMember = "idholding"
cbmaestro.DataSource = master.ObtenerMaestro().Tables("Maestro")
cbmaestro.DisplayMember = "nombre"
cbmaestro.ValueMember = "idmaestro"
cbsucursal.DataSource = gestionsol.obternersucursal().Tables("Sucursal")
cbsucursal.DisplayMember = "nombre"
cbsucursal.ValueMember = "idsucursal"
cbprioridad.Items.Add("Alta")
cbprioridad.Items.Add("Normal")
cbprioridad.Items.Add("Baja")
cbprioridad.SelectedIndex = 1
txtcantidad.Text = "1"
txtcantidad.Enabled = False
cbsucursal.Enabled = True
End Sub
Private Sub cbholding_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbholding.SelectionChangeCommitted
limpiarcb(cbsociedad)
cbsociedad.DataSource = gestionsol.obteneridsociedadporholding(cbholding.SelectedValue).Tables("Sociedad")
cbsociedad.DisplayMember = "nombre"
cbsociedad.ValueMember = "idsociedad"
If cbholding.SelectedValue = 1 Then
cbsucursal.Enabled = True
cbsucursal.DataSource = gestionsol.obternersucursal().Tables("Sucursal")
cbsucursal.DisplayMember = "nombre"
cbsucursal.ValueMember = "idsucursal"
cksap.Checked = True
cksmb.Checked = True
ckpersonas.Checked = True
Else
cbsucursal.Enabled = False
cksap.Checked = False
cksmb.Checked = False
ckpersonas.Checked = False
End If
End Sub
Public Sub limpiarcb(ByVal cboCombo As ComboBox)
cboCombo.DataSource = Nothing
cboCombo.ValueMember = Nothing
cboCombo.DisplayMember = Nothing
cboCombo.DropDownWidth = 1
cboCombo.SelectedValue = Nothing
cboCombo.Text = Nothing
Dim c, x As Double
c = cboCombo.Items.Count
While x < c
x = x + 1
cboCombo.Items.RemoveAt(x - 1)
End While
End Sub
las ya probadas:
cboCombo.DataSource = Nothing
cboCombo.ValueMember = Nothing
cboCombo.DisplayMember = Nothing
cboCombo.DropDownWidth = 1
cboCombo.SelectedValue = Nothing
cboCombo.Text = Nothing
cboCombo.items.clear()
Dim c, x As Double
c = cboCombo.Items.Count
While x < c
x = x + 1
cboCombo.Items.RemoveAt(x - 1)
End While
y ninguna funciona espero que me ayuden
codigo de la BD
' agregar primer combobox
Public Function obteneridholding() As DataSet
conn.Conectar()
Dim sql As String = "SELECT idholding,nombre FROM Holding"
cmd = New SqlCommand(sql, conn.conector)
dt.SelectCommand = cmd
dt.Fill(ds, "Holding")
conn.Desconectar()
Return ds
End Function
' agregar al combobox dependiente
Public Function obteneridsociedadporholding(ByVal id As Integer) As DataSet
holdin.id = id
obteneridholdingPorID(holdin)
Dim id2 As Integer
id2 = Convert.ToInt32(id)
Dim sql As String = "SELECT idsociedad,nombre FROM Sociedad WHERE idholding = '" & holdin.codigo & "'"
conn.Conectar()
cmd = New SqlCommand(sql, conn.conector)
dt.SelectCommand = cmd
dt.Fill(ds, "Sociedad")
Return ds
conn.Desconectar()
End Function
' codigo del formulario
Private Sub IngresoInfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cbholding.DataSource = gestionsol.obteneridholding().Tables("Holding")
cbholding.DisplayMember = "nombre"
cbholding.ValueMember = "idholding"
cbmaestro.DataSource = master.ObtenerMaestro().Tables("Maestro")
cbmaestro.DisplayMember = "nombre"
cbmaestro.ValueMember = "idmaestro"
cbsucursal.DataSource = gestionsol.obternersucursal().Tables("Sucursal")
cbsucursal.DisplayMember = "nombre"
cbsucursal.ValueMember = "idsucursal"
cbprioridad.Items.Add("Alta")
cbprioridad.Items.Add("Normal")
cbprioridad.Items.Add("Baja")
cbprioridad.SelectedIndex = 1
txtcantidad.Text = "1"
txtcantidad.Enabled = False
cbsucursal.Enabled = True
End Sub
Private Sub cbholding_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbholding.SelectionChangeCommitted
limpiarcb(cbsociedad)
cbsociedad.DataSource = gestionsol.obteneridsociedadporholding(cbholding.SelectedValue).Tables("Sociedad")
cbsociedad.DisplayMember = "nombre"
cbsociedad.ValueMember = "idsociedad"
If cbholding.SelectedValue = 1 Then
cbsucursal.Enabled = True
cbsucursal.DataSource = gestionsol.obternersucursal().Tables("Sucursal")
cbsucursal.DisplayMember = "nombre"
cbsucursal.ValueMember = "idsucursal"
cksap.Checked = True
cksmb.Checked = True
ckpersonas.Checked = True
Else
cbsucursal.Enabled = False
cksap.Checked = False
cksmb.Checked = False
ckpersonas.Checked = False
End If
End Sub
Public Sub limpiarcb(ByVal cboCombo As ComboBox)
cboCombo.DataSource = Nothing
cboCombo.ValueMember = Nothing
cboCombo.DisplayMember = Nothing
cboCombo.DropDownWidth = 1
cboCombo.SelectedValue = Nothing
cboCombo.Text = Nothing
Dim c, x As Double
c = cboCombo.Items.Count
While x < c
x = x + 1
cboCombo.Items.RemoveAt(x - 1)
End While
End Sub
Valora esta pregunta


0