Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2002
    Messaggi
    4,127

    [VB .NET] DataGridComboBoxColumn

    ho un problema con la classe personalizzata DataGridComboBoxColumn.
    Funziona benissimo se nella query sql utilizzo una sola tabella, invece dà errore se uso un INNER JOIN!
    Codice:
    codice:
    Imports System.Data.OleDb
    Public Class Index
        Inherits System.Windows.Forms.Form
        Dim DS As DataSet
        Dim MyAdapter As OleDbDataAdapter
    
    #Region " Codice generato da Progettazione Windows Form "
    'bla bla bla
    #End Region
    
        Private Sub Index_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Try
            Dim MyConnection As OleDbConnection
            MyConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\vs project\Tarcisio\conti.mdb;")
            DS = New DataSet
            MyAdapter = New OleDbDataAdapter
            MyAdapter.SelectCommand = New OleDbCommand("SELECT * FROM [tblGruppo] INNER JOIN [tblSottoGruppo] ON [tblGruppo].[ID] = [tblSottoGruppo].[IDGruppo]", MyConnection)
            'MyAdapter.SelectCommand = New OleDbCommand("SELECT * FROM [tblGruppo]", MyConnection)
            MyAdapter.Fill(DS, "[tbl]")
            DataGrid1.SetDataBinding(DS, "[tbl]")
            DataGrid1.DataSource = DS.Tables("[tbl]")
            Funzioni()
            'MyAdapter.Dispose()
            'DS.Dispose()
            'MyConnection.Dispose()
            'Catch Ex As Exception
            'Me.lblIntroduzione.Text = Ex.Message
            'End Try
        End Sub
        Sub Funzioni()
            'Dim cm As CurrencyManager
            'cm = Me.BindingContext(DataGrid1.DataSource)
            'CType(cm.List, DataView).AllowNew = False
            '**********************
            'DataGrid1.Enabled = False
            Dim ts1 As New DataGridTableStyle
            ts1.MappingName = "[tbl]"
            'ts1.AllowSorting = False
    
            Dim dCol As New DataGridTextBoxColumn
            dCol.MappingName = "tblSottoGruppo.Motivazione"
    
            Dim dCol2 As New DataGridComboBoxColumn(New ComboValueChanged(AddressOf MyComboValueChanged))
            dCol2.MappingName = "tblGruppo.Motivazione"
            dCol2.ColumnComboBox.DisplayMember = dCol2.MappingName
            dCol2.ColumnComboBox.DataSource = DS.Tables("[tbl]").Columns("tblGruppo.Motivazione").Table '.DataSet
            dCol2.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList
    
            ts1.PreferredRowHeight = (dCol2.ColumnComboBox.Height + 3)
            ts1.GridColumnStyles.Add(dCol)
            ts1.GridColumnStyles.Add(dCol2)
            ts1.DataGrid = DataGrid1
            DataGrid1.TableStyles.Clear()
            DataGrid1.TableStyles.Add(ts1)
            '/*********************
        End Sub
        Public Sub MyComboValueChanged(ByVal rowChanging As Integer, ByVal newValue As Object)
    
        End Sub
    End Class
    codice:
    Public Class DataGridComboBoxColumn
        Inherits DataGridTextBoxColumn
        Public WithEvents ColumnComboBox As ComboBox
        Private WithEvents _source As CurrencyManager
        Private _rowNum As Integer
        Private _isEditing As Boolean
        Private _valueChanging As ComboValueChanged
        Public Shared _RowCount As Integer
        'Fields
        'Constructors
        'Events
        'Methods
        Shared Sub New()
    
        End Sub
    
        Public Sub New(ByVal valueChanging As ComboValueChanged)
            MyBase.New()
            _isEditing = False
            _RowCount = -1
            _valueChanging = valueChanging
            ColumnComboBox = New ComboBox
            AddHandler ColumnComboBox.Leave, New EventHandler(AddressOf LeaveComboBox)
            AddHandler ColumnComboBox.Enter, New EventHandler(AddressOf ComboMadeCurrent)
            AddHandler ColumnComboBox.SelectedIndexChanged, New System.EventHandler(AddressOf ComboIndexChanged)
    
        End Sub
        Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal readOnly1 As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
    
            'on very first pass, set this static member to number of items in tables
            'check to see if we are on the new row
            If (_RowCount = -(1)) Then
                _RowCount = source.Count
            End If
            If (_RowCount < source.Count) Then
                ' remove the one that has been added
                ' and add a new to sync 
                source.RemoveAt((source.Count - 1))
                source.AddNew()
            End If
            _RowCount = source.Count
            MyBase.Edit(source, rowNum, bounds, readOnly1, instantText, cellIsVisible)
            _rowNum = rowNum
            _source = source
            ColumnComboBox.Parent = Me.TextBox.Parent
            ColumnComboBox.Location = Me.TextBox.Location
            ColumnComboBox.Size = New Size(Me.TextBox.Size.Width, ColumnComboBox.Size.Height)
            RemoveHandler ColumnComboBox.SelectedIndexChanged, New System.EventHandler(AddressOf ComboIndexChanged)
            ColumnComboBox.Text = Me.TextBox.Text
            AddHandler ColumnComboBox.SelectedIndexChanged, New System.EventHandler(AddressOf ComboIndexChanged)
            Me.TextBox.Visible = False
            ColumnComboBox.Visible = True
            ColumnComboBox.BringToFront()
            ColumnComboBox.Focus()
    
        End Sub
        Protected Overloads Overrides Function Commit(ByVal dataSource As CurrencyManager, ByVal rowNum As Integer) As Boolean
    
            If _isEditing Then
                _isEditing = False
                SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text)
            End If
            Return True
    
        End Function
        Private Sub ComboIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    
            _valueChanging(_rowNum, ColumnComboBox.Text)
    
        End Sub
        Private Sub ComboMadeCurrent(ByVal sender As Object, ByVal e As EventArgs)
    
            _isEditing = True
    
        End Sub
        Private Sub LeaveComboBox(ByVal sender As Object, ByVal e As EventArgs)
    
            If _isEditing Then
                SetColumnValueAtRow(_source, _rowNum, ColumnComboBox.Text)
                _isEditing = False
                Invalidate()
            End If
            ColumnComboBox.Hide()
    
        End Sub
    End Class
    Public Delegate Sub ComboValueChanged(ByVal changingRow As Integer, ByVal newValue As Object)
    è un po' lunghetto il codice, però non saprei dove mettere mani per risolvere l'errore. Quando provo a selezionare la colonna ComboBox mi viene:
    codice:
    Eccezione non gestita di tipo "System.ArgumentException" in system.windows.forms.dll
    
    Informazioni aggiuntive: Impossibile creare un elenco di elementi figlio per il campo tblGruppo.
    
    SULLA RIGA:
    ColumnComboBox.Parent = Me.TextBox.Parent
    grazie!

  2. #2
    Utente di HTML.it
    Registrato dal
    Sep 2002
    Messaggi
    4,127
    stesso errore anche utilizzando questo codice:
    http://dobon.net/vb/dotnet/datagrid/...dcombobox.html

    e se non uso l'inner join funziona perfettamente VVoVe:

  3. #3
    Utente di HTML.it
    Registrato dal
    Sep 2002
    Messaggi
    4,127
    faccio un up!

  4. #4
    Utente di HTML.it
    Registrato dal
    Sep 2002
    Messaggi
    4,127
    provo a riproporre la domanda, visto che ancora non ne esco..
    se possibile guardate il controllo del secondo post, lo preferisco a quello del primo, anke se è del tutto equivalente il risultato (almeno credo )

  5. #5
    Utente di HTML.it
    Registrato dal
    Sep 2002
    Messaggi
    4,127
    ennesimo up.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.